phpemailzend-frameworkattachmentaol

Zend Framework: Some email users get errors when trying to open PDF attachments?


I'm having a strange problem and not sure how to troubleshoot it. I have created a script in one of my Zend Framework controllers that allows an administrator to log in, upload a PDF, and send as an attachment to everyone subscribed to the mailing list. The problem is that some users report that they are unable to open the PDF attachment, that the file is corrupt. I think this is only happening to AOL users, but I'm not positive. Have you encountered this problem before? Or maybe it is not a problem with AOL, but something wrong with my code?

Here's the code that does the work:

Also, I'm using ZF version 1.6.0. Not sure if that is relevant.

//assuming the form is valid:
$table = new Subscribers();
$rowset = $table->fetchAll();
foreach ($rowset as $row) {
    $mail = new Zend_Mail();
    $mail->setBodyText($form->getElement('body')->getValue())
         ->setFrom('weekly-update@email.com', 'Weekly Update')
         ->addTo($row->email)
         ->setSubject($form->getElement('subject')->getValue());
    $fileLocation = $form->getElement('attachment')->getValue();
    $fileContents = file_get_contents($fileLocation);
    $attachment = $mail->createAttachment($fileContents);
    $attachment->filename = str_replace(Zend_Registry::get('config')->downloadsLocation . '/', '', $fileLocation);          
    $mail->send();
}

Solution

  • It appears (to me) that in this line of code:

    $attachment = $mail->createAttachment($fileContents);
    

    you likely need to add the additional header information available in the createAttachment method of the Zend_Mail framework::

    $attachment = $mail->createAttachment($fileContents,
                            Zend_Mime::DISPOSITION_INLINE);
    

    Many larger email providers are sticklers for strict adherence to good email policy (I've found).

    Play around with this and I'm sure you'll get it to work.