ballouta Posted August 9, 2008 Share Posted August 9, 2008 Hello kindly i need an expression that detects this string 'Delivery Status Notification' from the body of the message AND the email address returned: example: This is an automatically generated Delivery Status Notification Delivery to the following recipient failed permanently: cmzczxmcbn@hotmail.com Technical details of permanent failure: Google tried to deliver your message, but it was rejected by the recipient domain. We recommend contacting the other email provider for further information about the cause of this error. The error that the other server returned was: 550 550 Requested action not taken: mailbox unavailable (state 14). output needed: cmzczxmcbn@hotmail.com Thank you very much Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 9, 2008 Share Posted August 9, 2008 $str = 'Delivery to the following recipient failed permanently: cmzczxmcbn@hotmail.com Technical details of permanent failure:.. blah blah blah'; echo $str . '<br />'; // output main string to extract email from... $str = preg_match('#\b.+(@|(\[at\])).+\b#', $str, $match); echo $match[0]; This simple solution ought to do it. It matches anything on either side of either the @ character or [at] characters.. if you don't need to check for the [at] part, you can simply modify the pre_match line to the following: $str = preg_match('#\b.+@.+\b#', $str, $match); Cheers, NRG P.S This is a very loose version.. obvisouly, if people enter some gibberish with the @ symbol somewhere inbetween, this expression will find it.. it doesn't strictly search for properly formated emails... but I am assuming here that the email the system complains about is relatively proper in format... (then again, you know what they say about 'assume' though...) In any case, for general purposes, since the error message seems prefabbed, and again, assuming the email format is respectable, the above expression should work. Quote Link to comment Share on other sites More sharing options...
ballouta Posted August 9, 2008 Author Share Posted August 9, 2008 Hi nrg_alpha the code works correctly as I want. Thanks for the solution and for the notes. good luck Quote Link to comment Share on other sites More sharing options...
ballouta Posted August 20, 2008 Author Share Posted August 20, 2008 Hello again the previous solution didn't work on reading a messsage that contains more than 1 email address. for example, if the: <?php $message="This message was created automatically by mail delivery software. A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: a_s_metawie@hotmail.com a_s_m1976@hotmail.com a_s_m_a_r@hotmail.com a_s_d80@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_d80@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_s_alzrir@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_alzrir@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_s_a_@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_a_@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rola84@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rola84@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rogeena_aziz@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rogeena_aziz@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rodan@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rodan@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rock2222@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rock2222@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rivaldo@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rivaldo@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_ree_j@hotmail.com SMTP error from remote mail server after RCPT TO:<a_ree_j@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rafaee@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rafaee@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_r_n92@hotmail.com SMTP error from remote mail server after RCPT "; ?> the output is exactly: a_s_metawie@hotmail.com @ Would you please help. Note: no problem if it outputs repeated email address. but at least output them all without any other data. Thanks Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 21, 2008 Share Posted August 21, 2008 $message="This message was created automatically by mail delivery software. A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: a_s_metawie@hotmail.com a_s_m1976@hotmail.com a_s_m_a_r@hotmail.com a_s_d80@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_d80@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_s_alzrir@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_alzrir@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_s_a_@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_a_@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rola84@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rola84@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rogeena_aziz@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rogeena_aziz@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rodan@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rodan@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rock2222@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rock2222@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rivaldo@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rivaldo@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_ree_j@hotmail.com SMTP error from remote mail server after RCPT TO:<a_ree_j@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rafaee@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rafaee@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_r_n92@hotmail.com SMTP error from remote mail server after RCPT "; echo $message . '<br /><br />'; preg_match_all('#([^\s<]+@[^\s>]+)#', $message, $matches); for($i = 0, $total = count($matches[0]); $i < $total; $i++){ echo $matches[0][$i] . '<br />'; } Outputs: a_s_metawie@hotmail.com a_s_m1976@hotmail.com a_s_m_a_r@hotmail.com a_s_d80@hotmail.com a_s_d80@hotmail.com a_s_alzrir@hotmail.com a_s_alzrir@hotmail.com a_s_a_@hotmail.com a_s_a_@hotmail.com a_rola84@hotmail.com a_rola84@hotmail.com a_rogeena_aziz@hotmail.com a_rogeena_aziz@hotmail.com a_rodan@hotmail.com a_rodan@hotmail.com a_rock2222@hotmail.com a_rock2222@hotmail.com a_rivaldo@hotmail.com a_rivaldo@hotmail.com a_ree_j@hotmail.com a_ree_j@hotmail.com a_rafaee@hotmail.com a_rafaee@hotmail.com a_r_n92@hotmail.com Quote Link to comment Share on other sites More sharing options...
ballouta Posted August 21, 2008 Author Share Posted August 21, 2008 Thanks nrg_alpha for your help I can't find the problem in my code that outputs only one email address, kindly have a look on it <?php $str="This message was created automatically by mail delivery software. A message that you sent could not be delivered to one or more of its recipients. This is a permanent error. The following address(es) failed: a_s_metawie@hotmail.com a_s_m1976@hotmail.com a_s_m_a_r@hotmail.com a_s_d80@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_d80@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_s_alzrir@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_alzrir@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_s_a_@hotmail.com SMTP error from remote mail server after RCPT TO:<a_s_a_@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rola84@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rola84@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rogeena_aziz@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rogeena_aziz@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rodan@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rodan@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rock2222@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rock2222@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rivaldo@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rivaldo@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_ree_j@hotmail.com SMTP error from remote mail server after RCPT TO:<a_ree_j@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_rafaee@hotmail.com SMTP error from remote mail server after RCPT TO:<a_rafaee@hotmail.com>: host mx1.hotmail.com [65.54.245.8]: 550 Requested action not taken: mailbox unavailable a_r_n92@hotmail.com SMTP error from remote mail server after RCPT "; $str = preg_match('#\b.+(@|(\[at\])).+\b#', $str, $match); //echo $message . '<br />'; echo $match[0]; ?> Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 21, 2008 Share Posted August 21, 2008 Well, since the conditions are now different from the oringinal situation, simply use the exression I just posted the last time: preg_match_all('#([^\s<]+@[^\s>]+)#', $message, $matches); for($i = 0, $total = count($matches[0]); $i < $total; $i++){ echo $matches[0][$i] . '<br />'; } This expression will work better.. it doesn't check word bounderies. Instead, it uses a space or a '<' or '>' character as the bounderies. This is more effective in this situation. The reason you only get one email address ouput is that you are not looping through the $match array. If you notice in the above example, I made a for() loop. Basically, $i = 0, and I set a variable that is the total of the $matches array. Then, so long as $i < the total amount, echo out the current array (which in this case always starts with $matches[0]..so $matches[0][1] will ouput the first email address it found.. $matches[0][2] will ouput the second one, etc.. etc.. Cheers, NRG Quote Link to comment Share on other sites More sharing options...
ballouta Posted August 21, 2008 Author Share Posted August 21, 2008 Hi NRG The code is now working perfect. Many thanks for you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.