0

I am currently working on a project which processes an array of data which is imported from a csv. Once the data has been processed for that specific row, it will send an email to the email address specified for that row.

My client has a requirement that they need to know if an email bounces due to an incorrect email address being entered to the csv.

I currently send the emails by using PHP and an SMTP relay which we host, The only way that i have found to work so far is to scan the sending email address for any emails which contains the subject Undelivered Mail Returned to Sender.

This is working perfectly and i am getting a list of all of the emails which have bounced. My problem is that i need to get the email address which was entered incorrectly. If you see the screenshot below you can see that the script generates an HTML element/node which contains the email address.

HTML Element/Node

My question is, How to i grab/call this element?

I have tried print_ring the email object, unfortunately there is no array index with the email address which i require.

Please see my code below to see how i have gotten this far:

<?php $date = date('y-m-d'); $hostname = ''; $username = ''; $password = ''; $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to mailbox: ' . imap_last_error()); $emails = imap_search($inbox,'SUBJECT "Undelivered Mail Returned to Sender" ON "'.$date.'"'); if($emails){ rsort($emails); foreach($emails as $email){ $overview = imap_fetch_overview($inbox,$email,0); $message = imap_body($inbox, $email); print_r($overview); print_r($message); echo '<br>'; print_r($email); } } 

Thanks for your excellent help and support over the past.

Lewis

6
  • Do you have any code examples? The address has to be in there somewhere.. Commented Sep 26, 2017 at 13:50
  • Added the code on how i am grabbing the emails, The only place the email address is specified is in the html node which i referred to earlier. Thanks Commented Sep 26, 2017 at 13:55
  • 1
    I've never used imap_search(), but I see you are using the SUBJECT and ON keywords.. according to php.net/manual/en/function.imap-search.php, there is a TO keyword. Maybe you can do some magic with the query, some wildcarding maybe? Sorry if I'm pestering and not helping, but what does $emails contain? Commented Sep 26, 2017 at 14:02
  • No problem man, ill see if i can pull some wizardry on it! The $emails is an array of all of the emails which were returned by the .imap_search $email contains basic information such as, sent_date, sent_to and sent_from. Before you say sent_to sounds like what you are asking for. No, the email address specified here is the email address which contains all of the bounced emails. I appreciate your help! Commented Sep 26, 2017 at 14:08
  • Possible duplicate of How to catch bounced emails in PHP? Commented Sep 26, 2017 at 14:12

2 Answers 2

3

David and Kyrre managed to point me in the right direction, after a little more research i was able find that the email address is also located as being a Final-Recipient after a little research on how to grab the Final-Recipient i was able to grab what i needed.

To do this i did the following as discussed on this SA thread: Regular expression to parse Final-Recipient email header

 $message = imap_body($inbox, $email); $pattern = '/Final-Recipient: rfc822; (.*)/'; $matches = Array(); preg_match($pattern, $message, $matches); $email = $matches[1]; 
Sign up to request clarification or add additional context in comments.

Comments

0

As @Kyrre mentioned the imap_search() string, pretend it is a SQL query. Getting bounced email was answered here. There is also this sweet walk through that returns the entire email message content.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.