15.9 Parsing Mail Headers


You want to parse mail headers manually instead of using the imap_header() or imap_headers() function.

Technique

Use a regular expression:

 <?php $fp = fopen("mail_message.txt", "r") or die("Cannot open"); while ($line = fgets($fp, 1024)) {     if (preg_match("/^.*?:.*$/i", $line, $match)) {         list (, $header_name, $header_value) = $match;         $headers[trim($header_name)] = $header_value;     } else if (empty($line)) {         break;     } } ?> 

Comments

As far as the headers of a message are concerned , the header name and header value are separated by a : sign. This goes on until the end of the message, where there is an empty line before the body of the message. In the solution script, we match all the header names and values and then place name/value pairs into the $headers array. When the header section is over, we break out of the while loop.

Note that if you have the IMAP library installed, you can simply use the imap_header() function, which will return a psuedo-object of the headers for a particular message. For more information, see the documentation for imap_header() .



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net