ZF2-496: Attached documents to message objects are corrupt

Description

See http://zend-framework-community.634137.n4.nabble.com/… and this IRC log (23rd August):

{quote} : jurians: I was lookinag at your mail attachment issue. What's interesting is that Zend\Mime, functionally, hasn't changed between ZF1 and ZF2. The only difference is how messages work. I'm guessing there's some difference in how ZF1's mail class did attachments -- in ZF2, I'm simply saying that if we get a Mime\Message as the body to use it.

: jurians: I'll see if I can track it down quickly; if not, it may need to wait for a bugfix release.

: jurians: interesting. In ZF1, Zend_Mail extended Zend_Mime_Message. I'm stymied; no clue why it's not working in ZF1. Only thing I can think of is that Zend_Mail_Transport_Abstract was doing the part assembly (aggregating headers, boundaries, and content), and that Mime\Message doesn't do that naturally. {quote}

Task: attach a document (example a pdf) to the mail message and send it via the transport layer Expected result: attachment can be opened in an email client Actual result: attachment is corrupt and cannot be openend.

Similar code in Zend Framework 1 worked as expected and since then not much changed in the Zend\Mime part.

Example code to reproduce in ZF2:


$text = new Mime\Part('Hello world');
$text->type = 'text/plain';

$html = new Mime\Part('

Hello world

'); $html->type = 'text/html'; $data = file_get_contents('/path/to/my/file.pdf'); $pdf = new Mime\Part($data); $pdf->type = 'application/pdf'; $pdf->filename = 'My File.pdf'; $pdf->disposition = Mime\Mime::DISPOSITION_ATTACHMENT; $body = new Mime\Message; $body->setParts(array($text, $html, $pdf)); $message = new Message; $message->addTo('some@example', 'John Doe'); $message->setBody($body); $transport->send($message);

Comments

Found a work-a-round for this, the pdf is encoded '8bit'. If you add encoding as 'quoted-printable' or 'base64' the attachment will work.


$pdf->encoding = 'quoted-printable'; 

I suspect that ZF1 was making messages quoted-printable by default.

I have experimented with base64 encoded mime types, but my guess was to encode it myself.


$data = file_get_contents('/path/to/my/file.pdf');
$data = base64_encode($data);
$pdf  = new Mime\Part($data);
$pdf->type = 'application/pdf';
$pdf->filename = 'My File.pdf';
$pdf->encoding = Mime\Mime::ENCODING_BASE64;
$pdf->disposition = Mime\Mime::DISPOSITION_ATTACHMENT;

With the comment of Koen I suspect I don't need to encode the data myself. Because I switched to Swift Mailer, it's quite some work to revert it: in the next week I will test this without a base64_encode() from myself.

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf2/issues/2540