Class to parse email content recursively

emails, parsing, zend Add comments

I needed to parse emails coming to our system, and it turned out that there is no way to parse it recursively, since Multi-part emails can be nested one into another unlimited number of times.

So here is the class that parses and returns the first part of email, no matter how deep the recursion is. You are welcome!

<?php

/**
 * a service to parse an Multipart/Plain emails
 * supports base64 and quoted-printable formats
 * supports recursively nested parts (the most tricky part)
 * @author Alexander Skakunov
 * @since 2014-04-04
 */
class Service_Mail_Parser {

    /**
     * @param Zend_Mail_Message $message
     * @return string $content
     */
    public function parse(Zend_Mail_Message $message) {
        return $this->_getContent($message);
    }

    /**
     * gets content from the email object
     * @param Zend_Mail_Message $message
     * @return string $content
     * @throws Exception
     */
    protected function _getContent(Zend_Mail_Message $message) {
        if ($message->isMultipart()) {
            $content = $this->_getMultipartContent($message);
        }
        else {
            $content = $message->getContent();
        }

        if (empty($content)) {
            throw new Exception('Content is not parsable');
        }
        return $content;
    }

    /**
     * gets content from the multipart email
     * @param Zend_Mail_Message $message
     * @return string $content
     */
    protected function _getMultipartContent(Zend_Mail_Message $message) {
        $plainPart = $this->_getFirstPlainPart($message);
        if (empty($plainPart)) {
            return;
        }

        switch ($plainPart->contentTransferEncoding) {
            case 'base64':
                return base64_decode($plainPart->getContent());
            case 'quoted-printable':
                return quoted_printable_decode($plainPart->getContent());
        }
    }

    /**
     * recursively (!) gets the first plain content from the multipart email
     * i.e. the one that is not multipart
     * @param Zend_Mail_Message $message
     * @return Zend_Mail_Message $part
     */
    protected function _getFirstPlainPart(Zend_Mail_Part $message) {
        if (!$message->isMultipart()) {
            return $message;
        }
        $part = $message->getPart(1);
        return $this->_getFirstPlainPart($part);       
    }
}

This is how to use it:

$service = new Service_Mail_Parser;
$content = $service->parse($message);

Comments are closed.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in