Question

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();
}
Was it helpful?

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.

OTHER TIPS

I also had this problem.

I'd suggest you trace out the file stream info somehow. The issue with my app was that the $fileContents = file_get_contents($fileLocation); call wasn't getting the stream of the file properly, so this is where you might be falling down.

Try this:

$mail = new Zend_Mail();
...
var_dump($mail->send());

You should see a bunch of gibberish where the file stream comes out in the var_dump under the key:

["_content:protected"]=>
        string(37129) "%PDF-1.5
        etc...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top