views:

656

answers:

3

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('[email protected]', '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();
}
+1  A: 

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.

jerebear
would that cause the file to "be corrupted"? That's what my users are telling me when they try to open it, that the file is corrupt.
Andrew
The error usually says somethings like "Adobe could not open the file. It may be corrupted." And yeah, not having proper mime types can affect how the content is interpreted by the program. Sometimes it'll be viewed as a really long message that gets truncated (then corrupted)
jerebear
I've read up a little more about the MIME standard, so now your answer makes a lot more sense as to why I would be having this problem. I'll post my code to show what I needed to add for specific PDF MIME stuff. Thanks for your help!
Andrew
A: 

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...
Aaron Newton
A: 

I have a similar problem

Brecht Billiet