TechniqueUse 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; } } ?> CommentsAs 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() . |