Link to home
Start Free TrialLog in
Avatar of Chris Stormer
Chris Stormer

asked on

Parsing bouncing emails in PHP

Hi to everyone,

I have one difficult question about parsing bounced emails.I have manage to retrive info from bounced email such as:

From: MAILER-DAEMON@email.com (Mail Delivery System)
To: error@email.com
Date: Fri, 22 Sep 2006 16:58:13 -0500 (CDT)
Subject: Undelivered Mail Returned to Sender


Message body

The Postfix program : Host or domain name not found. Name service error for name=fs.gov type=A: Host not found

But I can't get final recipient(the recipient to whom email is sent).Can somebody explains to me how to retrive that info.

Thanks in advance

 
Avatar of TeRReF
TeRReF
Flag of Netherlands image

How do you retrieve the other data? Can you be a bit more specific?
Avatar of Chris Stormer
Chris Stormer

ASKER

I'm mostly using imap php functions...I have a class for receiving mails and function that I use is this one:

function getHeaders($mid) // Get Header info
      {
            $mail_header=imap_header($this->marubox,$mid);
            //print_r($mail_header);
            $sender=$mail_header->from[0];
            $sender_replyto=$mail_header->reply_to[0];
                  $mail_details=array(
                              'dated'=>$mail_header->MailDate,
                              'from'=>strtolower($sender->mailbox).'@'.$sender->host,
                              'fromName'=>$sender->personal,
                              'toOth'=>strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
                              'toNameOth'=>$sender_replyto->personal,
                              'subject'=>$mail_header->subject,
                              'to'=>strtolower($mail_header->toaddress)
                        );

            return $mail_details;
      }

If You need anything specific or You want to check whole class I could send it to You.
Does my comment make any sence?
Could someone at list point me to something cause I'm stack for two days and there is no way out..
Sorry, I didn't reply any sooner...
Try this:
function getHeaders($mid) // Get Header info
     {
          $mail_header=imap_header($this->marubox,$mid);
          //print_r($mail_header);
          $sender=$mail_header->from[0];
          $sender_replyto=$mail_header->reply_to[0];
          $recipient = $mail_header->to[0];
               $mail_details=array(
                         'dated'=>$mail_header->MailDate,
                         'from'=>strtolower($sender->mailbox).'@'.$sender->host,
                         'fromName'=>$sender->personal,
                         'toOth'=>strtolower($sender_replyto->mailbox).'@'.$sender_replyto->host,
                         'toNameOth'=>$sender_replyto->personal,
                         'subject'=>$mail_header->subject,
                         'to'=>strtolower($mail_header->toaddress),
                         'toRecipient'=>$recipient->personal
                    );

          return $mail_details;
     }

$recipient is an array like $sender, so toRecipient in the $mail_details array should contain the to email address...
I have try this one but it didn't helped.I have put print_r($recipient) just to see what kind of data I got and I got message:
stdClass Object ( [mailbox] => error [host] => email.com ).

Maybe I wasn't  clear in my needs so I will give You an example from the message I got from webmin readmail section:

This is form of message that I get from webmin read mail section.As U can see I have manage to retrieve From,To,Date,Subject and Message text.Only thing that I need right now is Failed delivery status(Final recipient) at the bottom of this message.


From MAILER-DAEMON@email.com (Mail Delivery System)
To error@email.com
Date Thu, 28 Sep 2006 16:26:00 -0500 (CDT)
Subject Undelivered Mail Returned to Sender
 
Message text  
 
This is the Postfix program at host email.com.

I'm sorry to have to inform you that your message could not
be delivered to one or more recipients. It's attached below.

For further assistance, please send mail to <postmaster>

If you do so, please include this problem report. You can
delete your own text from the attached returned message.

   The Postfix program

<aemory@farmersagent.com>: host mxpool.postoffice.net[165.212.65.113] said: 550
    <aemory@farmersagent.com>... User not known (in reply to RCPT TO command)


 Failed delivery status
<b>Final recipient aemory@farmersagent.com<b>
Reason for failure host mxpool.postoffice.net[165.212.65.113] said:
Reporting mail server email.versatables.com

If You need anything else please tell me cause You are the only person that have some interest in my problem.
Thanx.

ASKER CERTIFIED SOLUTION
Avatar of TeRReF
TeRReF
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have manage to fix this problem.Thanx man You helped me a lot.
You're welcome :)