Jump to content

[SOLVED] Need Expression for returned emails


ballouta

Recommended Posts

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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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];


?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.