alistairphillips.com

I’m : a web and mobile developer based in the Manning Valley, Australia.


PHP and MIME emails

We are using dotProject as our Helpdesk system here in the office and to allow users to quickly and easily create new requests I set up a email2dotproject gateway. It was a simple bit of code that merely took the senders address and used that to look up the requestors details.

Once those were located I set the title value to what was in the subject and then put the body of email message in as the summary. Now that was working really nicely until people started sending emails in HTML format. And it got even worse when they decided to attach files!

Just think about what it's like having a huge amount of text representing a base64 encoded file in your helpdesk entry. Not nice!

Now I had been wanting to try and decode MIME email messages in PHP but never managed to get it to work at all. So I sat down and did some googling and came up with the following sites:

That article gives you a nice introduction to on how to save file attachments. Using that as a basis for my code I was then able to pull our the text and html versions of the text as well as save all attachment using the original filenames. Quite nice :)

Now that I've got this done I will be able to start work on a Wordpress plugin that will ease the "moblogging" experience. At the moment wp-mail.php is not really able to handle MIME emails and attachments to well - you need to write your emails in plain text otherwise they don't come out too well.

Will post the code as soon as I have a decent syntax highlighter here.

<?php
    include ("mimeDecode.php");

    // Initialization
    $params['include_bodies'] = true;
    $params['decode_bodies']  = true;
    $params['decode_headers'] = true;

    // Open the file and read in the input
    $filename   = "email.txt";
    $file_input = fopen($filename, "r");
    $input      = fread($file_input, filesize($filename));
    fclose ($file_input);

    $params['input'] = $input;

    // Perform the decode
    $structure = Mail_mimeDecode::decode($params);
    $xml = Mail_mimeDecode::getXML($structure);

    //print_r($structure);

    foreach ($structure->parts as $part)
    {
        // Check for the text only portition of the email
        echo "<b>ctype_primary = </b>".$part->ctype_primary."<br />";
        echo "<b>ctype_secondary = </b>".$part->ctype_secondary."<br />";

        if (($part->ctype_primary == "text") and ($part->ctype_secondary == "plain"))
        {
            echo "<b>Body = </b>".$part->body."<br />";
        }

        // only save if an attachment
        if (isset($part->disposition) and ($part->disposition=='attachment'))
        {
            // determine the filename
            $filename = $part->d_parameters['filename'];

            if ($filename != "")
            {
                echo "<p>going to save the file $filename</p>";
                $fp = fopen($filename, 'w');

                // write body
                fwrite($fp, $part->body);

                // close file
                fclose($fp);
            }
        }
    }
?>