1

I'm sending an email with some xheader.

When the recipient of the email replays to that email, i want to parse it, and get the content of that xheader from the mail i get by replay.

unfortunately, when i'm parsing the email i get back, i don't see my xheader. (I printed the whole email, and the xheader is not there)

How can i do that in PHP with Zend Framework (i'm using Zend_Mail_Storage_Imap)?

Code:

    $mail = new Zend_Mail_Storage_Imap(array(
    'host' => 'pop.gmail.com',
    'user' => 'a@gmail.com',
    'password' => 'a',
    'ssl' => 'SSL'
));
$count = $mail->countMessages();
$message = $mail->getMessage($count);   
print_r($message);

    //go through the message
    foreach(new RecursiveIteratorIterator($message) as $part){
        echo '*****************<br/>';
        print_r($part);
        echo '<br/>*****************<br/>';         
        //match parts content type to text/html - the one that maches is the message HTML body
        if (preg_match('/.*text\/html.*/', $part->contentType)){
            $body = $part->getContent();
        }

        $headers = $part->getHeaders();
        if (isset($headers['X-aHeader'])){
            echo $headers['X-aHeader'];
        }

Thanks, Boris.

Boris C
  • 669
  • 2
  • 7
  • 14
  • Where are you sending the mail from? Are you sure the header doesn't get removed by one of the mail servers involved? – Pekka May 16 '11 at 09:34
  • 1
    Yes, i'm sure. i tried to send the mail directly to the box from which im parsing the mail, and when iv'e printed the mail - the header was there. – Boris C May 16 '11 at 09:36
  • Well..i't might be removed by the email server used when replaying. guess i have nothing to do in that case, right? – Boris C May 16 '11 at 10:03
  • 2
    If it's present in the target box, then it should be there. It will however definitely be lost when replying. I don't think mail clients include the headers when replying. – Pekka May 16 '11 at 10:05

1 Answers1

0

Pekka gets points for the correct response here - X-headers in an original message will not necessarily be retained for any replies to that message. But, you're using Gmail, so you have another potential option.

Gmail allows plus addressing, so username@gmail.com will also receive mail for username+extra@gmail.com. If your X-aHeader content is alphanumeric, you can append it to your email address (e.g. a+headerdata@gmail.com). If your header content isn't alphanumeric, I'd recommend caching what you would put in the header locally on your system, and provide a uniqid as the email plus-address. When you receive a reply, you can use that uniqid to look up the data you cached locally.

This is a common technique for uniquely identifying recipients who reply to emails, or who perhaps bounce messages.

angrychimp
  • 696
  • 1
  • 6
  • 13