Solved

Extract email address from returned email

Posted on 2008-10-06
7
767 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:Lordgobbledegook
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:babuno5
ID: 22656719
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
 
LVL 4

Author Comment

by:Lordgobbledegook
ID: 22656847
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 22660977
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 22661186
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22661289
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
 
LVL 4

Author Closing Comment

by:Lordgobbledegook
ID: 31503707
Apologies for the long delay replying.  I ended up writing my own function (long story) but your solution also works.

Thank you kindly!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 23558875
Thanks for the points!  Glad you got it working, ~Ray
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question