Quantcast

Attachment within emails are having encoding(?) issues

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Attachment within emails are having encoding(?) issues

Jurian Sluiman-3
Hi all,

I try to attach some files to an email message. Previously we have had apps in ZF1 working fine, but in ZF2 my pdfs can't be opened in (for example) Gmail. There's no error shown, but they are completely blank. This is the code I use now:

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

$html = new Mime\Part($html);
$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('[hidden email]', 'John Doe');
$message->setBody($body);

$transport->send($message);

When I look at the md5 checksums from /path/to/my/file.pdf and the attached pdf in my email, they are both different. Also, the file sizes are not equal! 

With almost similar code, this worked in ZF1:

$mail = new Zend_Mail;
$mail->addTo('[hidden email]', 'John Doe');
$mail->setBodyHtml($html);
$mail->setBodyText($text);

$data = file_get_contents('/path/to/my/file.pdf');
$at = $mail->createAttachment($data);
$at->filename = 'My File.pdf';
$at->type = 'application/pdf';
$at->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;

$mail->send();

What is going differently in ZF2 than in ZF1?
--
Jurian Sluiman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Attachment within emails are having encoding(?) issues

Jurian Sluiman-3
2012/8/10 Jurian Sluiman <[hidden email]>
Hi all,

I try to attach some files to an email message. Previously we have had apps in ZF1 working fine, but in ZF2 my pdfs can't be opened in (for example) Gmail. There's no error shown, but they are completely blank. This is the code I use now:

[code]

When I look at the md5 checksums from /path/to/my/file.pdf and the attached pdf in my email, they are both different. Also, the file sizes are not equal! 

With almost similar code, this worked in ZF1:

[code]

What is going differently in ZF2 than in ZF1?

I am guessing now Zend\Mime\Mime adds additional characters, like it is described in the RFC 2045 [1]: "The encoded output stream must be represented in lines of no more than 76 characters each.".

So, if Zend\Mime\Mime adds new line feeds after every 76 characters (even the output of a file_get_contents()), it is reasonable the file size of the email attachment increases (with respect of the pdf on the server's file system) and some email clients cannot read the attachment correctly anymore.

Any other ZF2 developer who's able to confirm this or has had a similar problems?

--
Jurian Sluiman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Attachment within emails are having encoding(?) issues

Jurian Sluiman-3
2012/8/11 Jurian Sluiman <[hidden email]>
2012/8/10 Jurian Sluiman <[hidden email]>
Hi all,

I try to attach some files to an email message. Previously we have had apps in ZF1 working fine, but in ZF2 my pdfs can't be opened in (for example) Gmail. There's no error shown, but they are completely blank. This is the code I use now:

[code]

When I look at the md5 checksums from /path/to/my/file.pdf and the attached pdf in my email, they are both different. Also, the file sizes are not equal! 

With almost similar code, this worked in ZF1:

[code]

What is going differently in ZF2 than in ZF1?

I am guessing now Zend\Mime\Mime adds additional characters, like it is described in the RFC 2045 [1]: "The encoded output stream must be represented in lines of no more than 76 characters each.".

So, if Zend\Mime\Mime adds new line feeds after every 76 characters (even the output of a file_get_contents()), it is reasonable the file size of the email attachment increases (with respect of the pdf on the server's file system) and some email clients cannot read the attachment correctly anymore.

Any other ZF2 developer who's able to confirm this or has had a similar problems?


As a follow up, I spoke to Matthew about this and he could confirm this. If any other users are having trouble with this, it will be fixed in a bugfix release after 2.0.0 (and hopefully 2.0.1 or so).

The issue to track progress is located here: http://framework.zend.com/issues/browse/ZF2-496
In the meantime I changed my application to use Swift Mailer (http://swiftmailer.org) which does the job too, but I will be happy to revert my app to Zend\Mail again when attachments are working again.
--
Jurian Sluiman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Attachment within emails are having encoding(?) issues

weierophinney
Administrator
-- Jurian Sluiman <[hidden email]> wrote
(on Thursday, 23 August 2012, 05:49 PM +0200):

> 2012/8/11 Jurian Sluiman <[hidden email]>
>     2012/8/10 Jurian Sluiman <[hidden email]>
>         I try to attach some files to an email message. Previously we have had
>         apps in ZF1 working fine, but in ZF2 my pdfs can't be opened in (for
>         example) Gmail. There's no error shown, but they are completely
>         blank. This is the code I use now:
>
>         [code]
>
>         When I look at the md5 checksums from /path/to/my/file.pdf and the
>         attached pdf in my email, they are both different. Also, the file sizes
>         are not equal!
>
>         With almost similar code, this worked in ZF1:
>
>         [code]
>
>         What is going differently in ZF2 than in ZF1?
>
>
>     I am guessing now Zend\Mime\Mime adds additional characters, like it is
>     described in the RFC 2045 [1]: "The encoded output stream must be
>     represented in lines of no more than 76 characters each.".
>    
>     So, if Zend\Mime\Mime adds new line feeds after every 76 characters (even
>     the output of a file_get_contents()), it is reasonable the file size of the
>     email attachment increases (with respect of the pdf on the server's file
>     system) and some email clients cannot read the attachment correctly
>     anymore.
>    
>     Any other ZF2 developer who's able to confirm this or has had a similar
>     problems?
>    
>     [1] http://tools.ietf.org/html/rfc2045
>
>
> As a follow up, I spoke to Matthew about this and he could confirm this.

Actually, I haven't written a test case confirming it. I _have_ looked
into the differences between the ZF1 and ZF2 APIs, however, and can see
that attachments are being handled differently. In ZF1, the abstract
transport assembles the parts, whereas in ZF2, we rely on the
Mime\Message to assemble them for us. I think the difference in behavior
likely lies in the differences in how those two operate.

I can confirm that line lengths have _not_ changed between the versions,
however -- both had them set at 76 -- nor have the line ending strings
(both use \r\n by default).

> If any other users are having trouble with this, it will be fixed in a
> bugfix release after 2.0.0 (and hopefully 2.0.1 or so).

> The issue to track progress is located here: http://framework.zend.com/issues/
> browse/ZF2-496
> In the meantime I changed my application to use Swift Mailer (http://
> swiftmailer.org) which does the job too, but I will be happy to revert my app
> to Zend\Mail again when attachments are working again.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
Loading...