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:

 

    [email protected]

 

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: [email protected]

 

Thank you very much

$str = 'Delivery to the following recipient failed permanently:
[email protected]

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.

  • 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:

  [email protected]

  [email protected]

  [email protected]

  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 
";
?>

 

the output is exactly:

[email protected]

@

 

Would you please help.

Note: no problem if it outputs repeated email address. but at least output them all without any other data.

 

Thanks

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

  [email protected]

  [email protected]

  [email protected]

  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
mailbox unavailable
  [email protected]
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:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

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:

  [email protected]

  [email protected]

  [email protected]

  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 

TO:<[email protected]>:
    host mx1.hotmail.com [65.54.245.8]: 550 Requested action not 

taken:
    mailbox unavailable
  [email protected]
    SMTP error from remote mail server after RCPT 
";


$str = preg_match('#\b.+(@|(\[at\])).+\b#', $str, $match);
//echo $message . '<br />';
echo $match[0];


?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.