#native_company# #native_desc#
#native_cta#

How to build a mailbot in PHP Page 3

By Michael Galloway
on November 6, 2002

Now we get into the simple loop that will feed the header data into our array. Mail headers are in a very simple format:
HEADERNAME: DATA
Where HEADERNAME is things like “Subject” or “Reply-To”, and DATA is the information. Now one thing to note here is that the format is separated by a colon and some white space. Unfortunately you cannot use a function like “split” because the DATA may have a colon too. (In fact it always will for certain headers, such as those relating to time). My solution is a simple one, that hasn’t failed, but could probably be tightened a bit.

<?php

while($line fgets($stdin)) {

    if(
$line != "n" && strlen($message_body) < 1) {

        if(
eregi("^([^:]+): (.*)",$line,$arr)) {

            

            
$message_head[strtolower($arr[1])] = trim($arr[2]);

        }

    } else 
$message_body .= "$line";

}

?>



At this point you have contained in simple data-structures the complete email message. You have parsed the headers and placed them into a hash table (associative array), and are ready to do the final parsing to determine your course of action. One little clean up action you might want to do before we search the “Subject” header is to format the “Reply-To” into a usable email address. The “Reply-To” is in the form: &[email protected]>
You need to simply remove the brackets. A simple way to do this is as follows:

<?php

eregi
("<([^>]+)>",$message_head["reply-to"],$arr);

$message_head["reply-to"] = $arr[1];

?>