IMAP to parse string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pmarg212

    IMAP to parse string?

    Greetings,

    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,
    Paul

  • william

    #2
    Re: IMAP to parse string?

    On Mon, 01 Jan 2007 22:06:55 -0800, pmarg212 wrote:
    Greetings,
    >
    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,
    Paul
    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

    Comment

    • Toby Inkster

      #3
      Re: IMAP to parse string?

      pmarg212 wrote:
      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?
      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
      messages.

      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:

      <?php

      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($bi ts);
      $body = implode("\n\n", $bits);
      unset($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(rtri m(array_shift($ bits)));
      $val = ltrim(implode(" :", $bits));

      /* Add it to $parsedhdrs array. Structure of
      * array is like:
      *
      * $parsedhdrs = array(
      * 'to' ='me@example.co m',
      * 'from' ='you@example.o rg',
      * 'received' =array(
      * 'yourhost.examp le.com on Saturday',
      * 'myhost.example .org on Friday night'
      * ),
      * 'content-type: text/plain'
      * );
      */
      if (is_array($pars edhdrs[$key]))
      {
      $parsedhdrs[$key][] = $val;
      }
      elseif (is_set($parsed hdrs[$key]))
      {
      $parsedhdrs[$key] = array($parsedhd rs[$key]);
      $parsedhdrs[$key][] = $val;
      }
      else
      {
      $parsedhdrs[$key] = $val;
      }
      }

      /* Decode body if encoded using quoted printable or base 64. */
      if (preg_match('/quoted.printabl e/i', $parsedhdrs['content-type']))
      $parsedbody = quoted_printabl e_decode($body) ;
      elseif (preg_match('/base64/i', $parsedhdrs['content-type']))
      $parsedbody = base64_decode($ body);
      else
      $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 = 'me@example.net ';
      mail($myaddress , 'Debugging', print_r($messag e, 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 ~ http://tobyinkster.co.uk/contact

      Comment

      Working...