IMAP to parse string?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I currently have incoming email piped to a php script using a .forward
file.  I would like to be able to parse the incoming mail, perform
operations, and then fire back an email (immediately) to the sender.
Is there any way that I can use the php IMAP functions on either the
php://stdin stream (the means through which the email is piped as raw
text) or on a plain string?

If not, are there any recommendations for what I can use to parse an
incoming email which may or may not be MIME?

Thanks for any help you can provide,

Re: IMAP to parse string?

On Mon, 01 Jan 2007 22:06:55 -0800, pmarg212 wrote:

Quoted text here. Click to load it

I would rather recommend you to use procmail + formail
and call a php script if needed.

depends if you really need to to do that in php or not

Re: IMAP to parse string?

pmarg212 wrote:

Quoted text here. Click to load it

IMAP?! IMAP is a mailbox access protocol which includes logging into a
mailbox, detecting folder structure, finding counts of unread messages,
retrieving particular messages and so on -- it's not for parsing e-mail

Exactly which parts of the message do you need to parse? Do you expect any
messages to have MIME types other than text/plain?

A quick and dirty example of parsing a text/plain message:


    function get_raw_message ()
        /* Your code to read message from stdin and
         * return a string. For simplicity, convert
         * "\r\n" to "\n" here too.

    function string2msg ($string)
        /* Header and body are separated by first occurance of
         * blank line. Split them apart.
        $bits = explode("\n\n", $string);
        $headers_raw = array_shift($bits);
        $body        = implode("\n\n", $bits);

        /* Header lines may be folded. Unfold them. */
        $headers = preg_replace('/\n\s+/', ' ', $headers_raw);

        /* For each header line... */
        $headers = explode("\n", $headers);
        foreach ($headers as $h)
            /* Split on first colon. */
            $bits = explode(":", $h);
            $key  = strtolower(rtrim(array_shift($bits)));
            $val  = ltrim(implode(":", $bits));
            /* Add it to $parsedhdrs array. Structure of
             * array is like:
             *     $parsedhdrs = array(
             *         'to' => '',
             *         'from' => '',
             *         'received' => array(
             *             ' on Saturday',
             *             ' on Friday night'
             *         ),
             *         'content-type: text/plain'
             *     );
            if (is_array($parsedhdrs[$key]))
                $parsedhdrs[$key][] = $val;
            elseif (is_set($parsedhdrs[$key]))
                $parsedhdrs[$key] = array($parsedhdrs[$key]);
                $parsedhdrs[$key][] = $val;
                $parsedhdrs[$key] = $val;

        /* Decode body if encoded using quoted printable or base 64. */
        if (preg_match('/quoted.printable/i', $parsedhdrs['content-type']))
            $parsedbody = quoted_printable_decode($body);
        elseif (preg_match('/base64/i', $parsedhdrs['content-type']))
            $parsedbody = base64_decode($body);
            $parsedbody = $body;

        /* Return everything that could possibly be useful to a script. */
        return array(
            'headers'     => $parsedhdrs,
            'body'        => $parsedbody,
            'raw_headers' => $headers_raw,
            'raw_body'    => $body,
            'raw_message' => $string

    /* Debugging */
    $message = string2msg(get_raw_message());
    $myaddress = '';
    mail($myaddress, 'Debugging', print_r($message, TRUE));

I haven't tested this, but it ought to work. (Probably forgot a semi-colon
or two.)

Toby A Inkster BSc (Hons) ARCS
Contact Me  ~

Site Timeline