dmertl.com

zend_mail_message

Using Zend to parse raw SMTP email data

March 11, 2009 by dmertl 3 Comments

I needed to create a script today that takes raw SMTP input from stdin. The project is running on Zend and they have a library for email. However, their documentation is geared toward getting that email from a mailserver. As far as I could find there wasn’t an example of how to create an email object out of the raw SMTP data.

Create a Zend_Mail_Message from the raw string.

$email = new Zend_Mail_Message(array('raw' => $email_str));

To grab the subject:

$email->subject;

All I wanted was the body from the text/plain piece of the email. If you’ve got a multi-part message it’s a little tricky. Here’s what I compiled from Zend’s documentation:

if($email->isMultipart()) {
  foreach (new RecursiveIteratorIterator($email) as $part) {
    try {
      if (strtok($part->contentType, ';') == 'text/plain') {
        $body = trim($part);
        break;
      }
    } catch (Zend_Mail_Exception $e) { // ignore }
  }
  if(!$body) {
    //Error
  }
} else {
  $body = trim($email->getContent());
}
Posted in: Zend Tagged: email, parse, php, raw, smtp, Zend, zend_mail_message

Blogroll

  • Antarctica
  • Non-coding stuff
  • Photos
  • Rainbows

Recent Posts

  • Resizing optimized animated GIFs in Photoshop
  • Saving an animated GIF in Photoshop with “combine” instead of “replace” frames
  • Mimicking Django Admin Change Form Styling in a Custom Form
  • Installing custom Plex plugins on a Drobo 5N
  • Bug in CakePHP Containable Behavior, Related Records not Being Returned When Parent Contains a Single Field

Categories

  • CakePHP
  • Canon
  • Django
  • Doctrine
  • iPhone
  • mac
  • Magento
  • MS Access
  • MSSQL
  • Photography
  • Python
  • SQL
  • Uncategorized
  • Zend

Tags

@@IDENTITY adapter authentication breadcrumbs CakePHP Canon cocoa Doctrine email eos utility errors iPhone leopard lost cd mac Magento MS Access MS SQL mysql mysqli nosuchtable os x override parse pdo_mysql.php php programming raw redirect smtp SQL sqlite SQL Server triggers updater validation xcode Zend zend_mail_message

Copyright © 2023 dmertl.com.

Omega WordPress Theme by ThemeHall