Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Issue with Multiple Attachments (First Attachment OK, Second Attachment Corrupted) #666

Closed
dahomz opened this issue Sep 24, 2015 · 7 comments

Comments

@dahomz
Copy link

dahomz commented Sep 24, 2015

I've run into a problem when attempting to attach two files (pdf and csv) into an e-mail. Whichever file I attach first is OK, but the next attachment is corrupted. I've tested/confirmed both single attachments can be sent correctly; the issue only occurs when both files are attached to the same e-mail. Here's a simplified example of my code.

        $attachment_csv = \Swift_Attachment::newInstance()
            ->setFilename('file.csv')
            ->setContentType('text/csv')
            ->setBody($this->renderView('file.csv.twig'));

        $attachment_pdf = \Swift_Attachment::newInstance()
            ->setFilename('file.pdf')
            ->setContentType('application/pdf')
            ->setBody($this->get('spraed.pdf.generator')->generatePDF($this->renderView('file.pdf.twig')));


        $message = \Swift_Message::newInstance()
            ->setSubject('Message Subject')
            ->setFrom('noreply@me.com')
            ->setTo('me@me.com')
            ->setBody($this->renderView('message.html.twig'),'text/html' )
            ->attach($attachment_pdf)
            ->attach($attachment_csv);

        $this->get('mailer')->send($message);

Documentation refers to multiple attaching files from disk, wondered I'm using the attachment instance incorrectly or whether there is an issue.

Assistance appreciated, thanks.

@voku
Copy link

voku commented Sep 25, 2015

I don't know exactly, but in this example "fromPath()" is used?! https://github.com/suzuki/swiftmailer-samples/blob/master/src/120-message-attach.php

@dahomz
Copy link
Author

dahomz commented Sep 25, 2015

Thanks Voku, you are correct - to add attachments from disk you can use the fromPath method. I tested the example and unfortunately it generates the same error.

In my case both attachments are derived from a HTML query report (one is converted to PDF) - so I would prefer not to store them to disk before attaching.

    $attachment_csv = \Swift_Attachment::fromPath(
            '/assets/report.csv',
            'text/csv'
        );

    $attachment_pdf = \Swift_Attachment::fromPath(
            '/assets/report.pdf',
            'application/pdf'
        );


    $message = \Swift_Message::newInstance()
        ->setSubject('Message Subject')
        ->setFrom('noreply@me.com')
        ->setTo('me@me.com')
        ->setBody($this->renderView('message.html.twig'),'text/html' )
        ->attach($attachment_pdf)
        ->attach($attachment_csv);

    $this->get('mailer')->send($message);

Another thing I have just noticed is that attachments are the same size and should not be. i.e. the PDF file should be larger than the CSV. I checked the contents and it looks like the second file is just a duplicate of the first.

So I'm guessing that the way that I'm attaching multiple files is not correct. Maybe it's not possible with Swiftmailer as there are no examples of multiple attachments in the documentation. :-(

@dahomz
Copy link
Author

dahomz commented Oct 5, 2015

As a painful workaround for this I ended up sending two separate emails with single attachments.

I wonder if anyone else has been successful in sending multiple attachments?

I'm suspicious that the second attachment is a copy of the first, so maybe the second Swift_Attachment instance was not created correctly.

@voku
Copy link

voku commented Oct 6, 2015

@dahomz I have added a test, but it didn't work as expected ... maybe you can take a look at the test, there is something still missing in the reg-ex ... :/

voku pushed a commit to voku/swiftmailer that referenced this issue Oct 16, 2015
@bolint
Copy link

bolint commented Nov 17, 2015

It's a problem on Windows os.
The quick fix for that is to disable the cache before attach the files.

Swift_Preferences::getInstance()->setCacheType('null');

Reletad issue: #602

@n0rbyt3
Copy link

n0rbyt3 commented May 12, 2016

I have the same problem, but could identify the issue. If the messages are spooled, the message will be cloned. And during cloning a new _cacheKey is generated. BUT the key is generated in another way than inside the constructor.

inside Swift_Mime_SimpleMimeEntity:__construct():

$this->_cacheKey = md5(uniqid(getmypid().mt_rand(), true));

inside Swift_Mime_SimpleMimeEntity:__clone():

$this->_cacheKey = uniqid();

which resulted for me to:

string(37) "old: 777fbc62960334c26b67eed702aa93ad"
string(18) "new: 57347b49dcdf8"

string(37) "old: 7ac80fd2f64f313dc4acc10bf087d728"
string(18) "new: 57347b49dcdf8"

string(37) "old: 92f1ae6836c5049d68d6bf56ea7f90c9"
string(18) "new: 57347b49dcdf8"

string(37) "old: 86d0cd18a8fbeec0ede400b3e9d34b74"
string(18) "new: 57347b49dcdf8"

string(37) "old: 39cad7f3564ddd34076bbb3837eead4f"
string(18) "new: 57347b49dcdf8"

string(37) "old: 316a631354da94049dcfc6f2295bfc52"
string(18) "new: 57347b49dcdf8"

As you can see the same ID is generated, so the first image or attachment will be ok and the data of all other attachments and images are linked to the first rendered one.

EDIT: v5.4.2 fixes it.

@fabpot
Copy link
Member

fabpot commented May 12, 2016

It has been fixed in bf40b56

@fabpot fabpot closed this as completed May 12, 2016
makasim pushed a commit to formapro-forks/swiftmailer that referenced this issue Jul 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants