Extract email address from returned email

Hi,

I am creating a newsletter script and need to automatically process bounced emails.  The good news is that most of the functions are complete.

At the present moment, every email sent is from a unique email address. This makes it easier to recognise which email addresses are bouncing.

newsletter1@mydomain.com ---> recipient 1
newsletter23@mydomain.com ---> recipient 23
newsletter47@mydomain.com ---> recipient 47, etc

At present, bounced emails are piped to a PHP script (below).  How do I extract the newsletter47@mydomain.com part of the bounced email?  Or better yet, just the "47", "23", "1" part.

The $emailcontents variable holds the entire contents of the bounced email.

Thank you all!
#!/usr/local/bin/php -q
<?php
 
 
// open email file
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $emailcontents .= fread($fd, 1024);
}
fclose($fd);

Open in new window

LVL 4
LordgobbledegookAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ray PaseurConnect With a Mentor Commented:
Since that's kind of clunky code, I would probably wrap it into a function, like this...

best regards, ~Ray
<?php // RAY_header_from.php
 
$msg_headers = 'Microsoft Mail Internet Headers Version 2.0
Received: from tiger.npc.natpresch.org ([192.168.0.3]) by mail.natpresch.org with Microsoft SMTPSVC(6.0.3790.3959);
	 Tue, 7 Oct 2008 10:13:09 -0400
Received: from mailscanner.concentus.net ([64.94.64.6]) by tiger.npc.natpresch.org with Microsoft SMTPSVC(6.0.3790.3959);
	 Tue, 7 Oct 2008 10:13:08 -0400
Return-Path: jaydeecarr@verizon.net
X-Envelope-From: jaydeecarr@verizon.net
X-Envelope-To: KBrinkley@NationalPres.org
Received: From vms172071pub.verizon.net (206.46.172.71) by mailscanner.concentus.net (MAILFOUNDRY) id oYb14JR5Ed2QfAAw; Tue, 7 Oct 2008 14:10:01 -0000 (GMT)
Received: from [192.168.1.101] ([76.95.55.186]) by vms172071.mailsrvcs.net
 (Sun Java System Messaging Server 6.2-6.01 (built Apr  3 2006))
 with ESMTPA id <0K8D005X2GSB7181@vms172071.mailsrvcs.net>; Tue,
 07 Oct 2008 09:12:12 -0500 (CDT)
Date: Tue, 07 Oct 2008 10:12:04 -0400
From: JEANNE CARR <jaydeecarr@verizon.net>
Subject: NPC Web Page: www.nationalpres.org/sermon.php?d=2008-10-05+0000
To: KBrinkley@NationalPres.org
Cc: RPaseur@NationalPres.org
Message-id: <C4C881E2-2224-4185-8971-1704F784231E@verizon.net>
MIME-version: 1.0 (Apple Message framework v753.1)
X-Mailer: Apple Mail (2.753.1)
Content-type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
Content-transfer-encoding: 7bit
X-OriginalArrivalTime: 07 Oct 2008 14:13:08.0957 (UTC) FILETIME=[D31FB8D0:01C92886]
';
 
 
 
 
 
function get_my_email_thing($msg_headers, $needle='X-Envelope-From:', $delim='?') {
 
// TURN ALL WHITESPACE OR EOL INTO MARKER CHARACTERS
	$msg_headers = trim(ereg_replace(" +", "$delim", $msg_headers));
	$msg_headers = trim(ereg_replace("\n", "$delim", $msg_headers));
 
// LOOK FOR THE X-FROM LINE TERMINATED BY THE MARKER CHARACTER
	$needle = $needle . $delim;
	$from_poz = strpos($msg_headers, $needle);
	if ($from_poz === FALSE) { die("No $needle"); }
 
// DISCARD UNUSED STUFF
	$msg_headers = substr($msg_headers, $from_poz+strlen($needle));
 
// LOOK FOR THE MARKER CHARACTER AT THE END OF THE X-FROM ADDRESS
	$end_poz = strpos($msg_headers, $delim);
 
// IF NOT BLANK, THERE ARE NOT MORE HEADERS AFTER THIS ONE, SAVE THE X-FROM ADDRESS
	if ($end_poz === FALSE) {
		$from_address = $msg_headers;
	} else {
		$from_address = substr($msg_headers,0,$end_poz);
	}
 
// RETURN ADDRESS
return $from_address;
}
 
 
 
 
 
echo get_my_email_thing($msg_headers);
 
 
 
?>

Open in new window

0
 
babuno5Commented:
use this regular expression to extract email addresses

/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/ig

use the function preg_match_all with the above regular expression
http://www.php.net/preg_match_all
0
 
LordgobbledegookAuthor Commented:
Thank you for your comment. The pattern will certainly match email addresses but the catch is that there is no standard "bounced email" format.

The bounced email often contains a number of email addresses which makes life difficult.  Is there a way of extracting an email that represents the "From" and "To" tags?

In the meantime I have implemented a crude workaround but it is only tested on bounced emails from 2 or 3 domains.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Ray PaseurCommented:
Some combination of strpos() to locate "From" or "To" coupled with a strpos() to locate the end-of-email delimiter should help you isolate the part of the message with the email address.  Then you can use substr() to pluck it out of the rest of the stuff.  After that, you can use the REGEX to clean up the address, then something like str_replace() to cull out the rest of the unnecessary stuff, leaving you with just the numbers.  HTH, ~Ray
0
 
Ray PaseurCommented:
Sampel here... ~Ray
<?php // RAY_header_from.php
 
$msg_headers = 'Microsoft Mail Internet Headers Version 2.0
Received: from tiger.npc.natpresch.org ([192.168.0.3]) by mail.natpresch.org with Microsoft SMTPSVC(6.0.3790.3959);
	 Tue, 7 Oct 2008 10:13:09 -0400
Received: from mailscanner.concentus.net ([64.94.64.6]) by tiger.npc.natpresch.org with Microsoft SMTPSVC(6.0.3790.3959);
	 Tue, 7 Oct 2008 10:13:08 -0400
Return-Path: jaydeecarr@verizon.net
X-Envelope-From: jaydeecarr@verizon.net
X-Envelope-To: KBrinkley@NationalPres.org
Received: From vms172071pub.verizon.net (206.46.172.71) by mailscanner.concentus.net (MAILFOUNDRY) id oYb14JR5Ed2QfAAw; Tue, 7 Oct 2008 14:10:01 -0000 (GMT)
Received: from [192.168.1.101] ([76.95.55.186]) by vms172071.mailsrvcs.net
 (Sun Java System Messaging Server 6.2-6.01 (built Apr  3 2006))
 with ESMTPA id <0K8D005X2GSB7181@vms172071.mailsrvcs.net>; Tue,
 07 Oct 2008 09:12:12 -0500 (CDT)
Date: Tue, 07 Oct 2008 10:12:04 -0400
From: JEANNE CARR <jaydeecarr@verizon.net>
Subject: NPC Web Page: www.nationalpres.org/sermon.php?d=2008-10-05+0000
To: KBrinkley@NationalPres.org
Cc: RPaseur@NationalPres.org
Message-id: <C4C881E2-2224-4185-8971-1704F784231E@verizon.net>
MIME-version: 1.0 (Apple Message framework v753.1)
X-Mailer: Apple Mail (2.753.1)
Content-type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
Content-transfer-encoding: 7bit
X-OriginalArrivalTime: 07 Oct 2008 14:13:08.0957 (UTC) FILETIME=[D31FB8D0:01C92886]
';
 
// TURN ALL WHITESPACE OR EOL INTO MARKER CHARACTERS
$msg_headers = trim(ereg_replace(" +", "?", $msg_headers));
$msg_headers = trim(ereg_replace("\n", "?", $msg_headers));
 
// LOOK FOR THE X-FROM LINE TERMINATED BY THE MARKER CHARACTER
$needle = 'X-Envelope-From:?';
$from_poz = strpos($msg_headers, $needle);
if ($from_poz === FALSE) { die("No $needle"); }
 
// DISCARD UNUSED STUFF
$msg_headers = substr($msg_headers, $from_poz+strlen($needle));
 
// LOOK FOR THE BLANK AT THE END OF THE X-FROM ADDRESS
$needle = '?';
$end_poz = strpos($msg_headers, $needle);
 
// IF NOT BLANK, THERE ARE NOT MORE HEADERS AFTER THIS ONE, SAVE THE X-FROM ADDRESS
if ($end_poz === FALSE) {
	$from_address = $msg_headers;
} else {
	$from_address = substr($msg_headers,0,$end_poz);
}
 
echo $from_address;
?>

Open in new window

0
 
LordgobbledegookAuthor Commented:
Apologies for the long delay replying.  I ended up writing my own function (long story) but your solution also works.

Thank you kindly!
0
 
Ray PaseurCommented:
Thanks for the points!  Glad you got it working, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.