1

This PHP function working just fine:

if (preg_match ("/<(\S+@\S+\w)>.*\n?.*55[0-9]/i",$body,$match)) {
echo "found!";
}

in

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:

xxx@hotmail.com
SMTP error from remote mail server after RCPT TO:<xxx@hotmail.com>:
host mx3.hotmail.com [65.54.188.72]: 550 Requested action not taken:
mailbox unavailable

But if I use the same php function in this case:

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:

xxx@yahoo.com
SMTP error from remote mail server after end of data:
host d.mx.mail.yahoo.com [209.191.88.254]: 554 delivery error:
dd This user doesn't have a yahoo.com account (xxxd@yahoo.com) [0] -  mta1010.mail.mud.yahoo.com

The preg_match function does not give any results. What am I doing wrong?

codaddict
  • 445,704
  • 82
  • 492
  • 529
Sergio
  • 1,207
  • 7
  • 28
  • 42

2 Answers2

4

You are basically looking for a email addressing enclosed in < and >.

Your first input has it but the second input does not: It has no email id in < > hence you don't get a match.

Edit: (after your comment):

Looking at the 2 samples given by you the email id appears between the string failed: and the error code starting with 55. You can make use of the preg_match for this as:

if(preg_match('/failed:.*?(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b).*55[\d]/is',$str,$m)) {
       echo "Bounced email ".$m[1]."\n";
}

Ideone link

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • So I have to remove "<" from the function? – Sergio Dec 01 '10 at 16:40
  • @Sergio: It's not very clear what you are looking for. The text given by you are failed messaged delivery messages. Are you looking for `xxx@hotmail.com` and `xxx@yahoo.com` ? If yes, you can look for the email pattern that follows the text `failed:`. – codaddict Dec 01 '10 at 16:45
  • This is the part of the script aimed to check bounced Emails using the text (errors) in bounced messages. What I need is the email address of the message that was sent and bounce back. In these cases xxx@hotmail.com and xxx@yahoo.com – Sergio Dec 01 '10 at 16:50
0

By default, preg_match doesn't support parsing of multiline string (String containing \n), so you gotta pass in extra parameter to match the whole string, which is m (multiline).

try

if (preg_match ("/<(\S+@\S+\w)>.*\n?.*55[0-9]/im",$body,$match)) {
echo "found!";
}
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123