PHP Classes

Empty header fields

Recommend this page to a friend!

      PHP MIME Email Message Parser  >  All threads  >  Empty header fields  >  (Un) Subscribe thread alerts  
Subject:Empty header fields
Summary:The data in From: and Return-Path: fields isn't always retrieved
Author:Mario Huizinga
Date:2007-05-17 15:02:40
Update:2007-09-17 09:28:52

  1. Empty header fields   Reply   Report abuse  
Picture of Mario Huizinga Mario Huizinga - 2007-05-17 15:02:40
Hello Manuel,

First: thanks for proving this class alowing people to decode mime mail without Pear.

My question/problem:
I was working on some content checking to filter out unwanted mail and noticed that in a testmail (sent from outlook via exchange) some returned header fields where empty.
(I used var_export ($decoded[$message]); instead of var_dump($decoded[$message]); in test_message_decoder.php)
Looking at the raw mailfile I found that in the From: and Return-Path: fields the text between the quotes was missing.

"Correct" decoded header fields:
From: "Lastname, Firstname" <MYMAIL@MYDOMAIN.COM>
Return-Path: "Lastname, Firstname" <MYMAIL@MYDOMAIN.COM>

Returning empty field data:

I have to add that browsing thrue the headers of lots of mail in my mailbox I could not find any header that had the working format for the Return-Path field.
It seems that when the (in this example) quoted part of these fiels is empty the class does not check if it can retrieve the content between the "<>".
(I also noticed that there is an extra trailing space returned by the class when there is data in front of the <> data in the two mentioned fields.)

In addition you may notice that "Lastname, Firstname" isn't an email address but (in this case) the name as displayed in the outlook addressbook.

In fact I'm wondering how to e.g. return an email with a header like this to the originator using the return or from data if the mail is rejected by my script.
Worst case I won't get any address data from the class and even when the quoted part exists it's possibly the addressbook name instead of the email address.

Any tips on how to proceed and access the data between <> in the two address fieds?



  2. Re: Empty header fields   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-05-17 16:31:17 - In reply to message 1 from Mario Huizinga
The return address is not necessarily passed in the message. That address is only informed to the receiving SMTP server during message transmission. Since you only get the headers, you need to rely on the from: header .

Currently this class does not decode headers to extract e-mail addresses from their values. That feature will be included in a future release.

Alternatively, the MIME composing and sending message class has a private function named GetRFC822Addresses that you can use to extract addresses from header values.

It is a private function, so it may change or even be removed. So, I recommend that you copy the copy the function code to your application so it is always there.

  3. Re: Empty header fields   Reply   Report abuse  
Picture of Mario Huizinga Mario Huizinga - 2007-05-18 08:25:18 - In reply to message 2 from Manuel Lemos
Thanks for your quick response Manuel,

While testing with a message send from a mobile phone I found out that when the email address includes a "+" the this address is returned by the class.

From: <+anynumber@MYDOMAIN.COM> does return "+anynumber@MYDOMAIN.COM" while From: <MYMAIL@MYDOMAIN.COM> doesn't.
I'm confused why the "+" in the address makes the difference.

I'll have a try with GetRFC822Addresses() and use it until you have time to implement extracting email adresses from their values.



  4. Re: Empty header fields   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-05-18 22:16:53 - In reply to message 3 from Mario Huizinga
If you are using var_dump to display the parsed message results in an HTML page, keep in mind that the browser may assume that anything between < and > is a tag and so not display any data between those characters.

It seems that if the first character is +, your browser does not interpret what is inside < > as an HTML tag.

Use HtmlSpecialChars function to encode < > to display it in an HTML page.

  5. Re: Empty header fields   Reply   Report abuse  
Picture of Mario Huizinga Mario Huizinga - 2007-05-20 00:42:11 - In reply to message 4 from Manuel Lemos
You are correct. My browser messed it up.
Running the script from the command line showed the mail addresses between <> as it should.


  6. Re: Empty header fields   Reply   Report abuse  
Picture of Alan Fullmer Alan Fullmer - 2007-09-17 09:28:52 - In reply to message 5 from Mario Huizinga
One could also use regex to accomplish this:

$regex = '/\b[A-Z0-9._%-]+?@[A-Z0-9._%-]+?\.[A-Z]{2,7}\b/iU';
preg_match ($regex, $msg_from, $msg_fromaddr_a);

Works pretty well. But just doing a <> match is probably easiest.