Save an email attachment using Zend Mail

To do some automated billing I needed to grab an email attachment off the exchange server. The Zend framework made it very easy to connect to the mail server and navigate files, and that part of the code came directly from the Zend Framework documentation. The issue that took the longest was saving the attachment. I could not figure out how to decode the attachment using the Zend framework. Let me know if you have a better way or if it helps you out.
NOTE: If you getting the mail from the INBOX you do not have to use the getFolders() function.

// Connecting with Imap
$mail = new Zend_Mail_Storage_Imap(
        array('host'     => 'SERVER',
              'user'     => 'USERNAME',
              'password' => 'PASSWORD'));

// Navigate to desired folder
$folder = $mail->getFolders()->INBOX->Info;

// Change to folder
$mail->selectFolder($folder);

// Loop through messages
foreach ($mail as $message)
{
 // Find desired message subject
 if($message->subject == 'SUBJECT')
 {
 // Check for attachment
 if($message->isMultipart())
 {
    $part = $message->getPart(2);
 }

 // Get the attacment file name
 $fileName = $part->getHeader('content-description');

 // Get the attachement and decode
 $attachment = base64_decode($part->getContent());

 // Save the attachment
 $fh = fopen($fileName, 'w');

 fwrite($fh, $attachment);

 fclose($fh);
 }
}
Advertisements

9 thoughts on “Save an email attachment using Zend Mail

  1. sorry for diggin up old topic:) it’s the only working example i guess
    can you tell me – how can i change default charset for attachment names/subjects
    any help is appreciated. thanks!

    1. Are you looking to change the charset for outgoing mail or reading incoming mail? Sorry but I’m not sure what your asking, but I will help anyway I can.

  2. sorry for the weird question:)
    Incoming email only. I’m mostly fine now cutting text part from =?UTF-8?B?*?= subjects i get and putting them into base64_decode().
    But I’m not sure it is the right solution.

  3. What if the email doesn’t have the content-description header? Is there another way of saving the attachment?

  4. @jason kerner Look for a “content-disposition” header. This example is a good start but in the crazy world of emails happen a lot of bizarre things.

  5. if you write this

    echo ‘

    ';
    print_r($part);
    echo '

    ‘;

    after $part = $message->getPart(2);
    you understand all it needs to be understood for all cases.

  6. Hi !
    Thanks for your post. I am using Zend framework to read emails from my gmail inbox and extract attachments from it. it works fine. however, I would like to get only those mails that were received in the last 1 hour. Is there a way to do that ?

    At present, ‘$mail->selectFolder($folder);’ selects all the mails from the specified folder and the iteration takes place for all the mails. I want that the Imap stream $mails should only have mails for the last 1 hour. How can I do that ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s