Now we get into the simple loop that will feed the header data into our array. Mail headers are in a very simple format:
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.
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: <firstname.lastname@example.org>
You need to simply remove the brackets. A simple way to do this is as follows: