You are here: Re: IMAP to parse string? « PHP Programming Language « IT news, forums, messages
Re: IMAP to parse string?

Posted by Toby Inkster on 01/03/07 11:37

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($bits);
$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(rtrim(array_shift($bits)));
$val = ltrim(implode(":", $bits));

/* Add it to $parsedhdrs array. Structure of
* array is like:
*
* $parsedhdrs = array(
* 'to' => 'me@example.com',
* 'from' => 'you@example.org',
* 'received' => array(
* 'yourhost.example.com on Saturday',
* 'myhost.example.org 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;
}
else
{
$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);
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($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 ~ http://tobyinkster.co.uk/contact

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация