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
Solved

Extract email address from returned email

Posted on 2008-10-06
7
746 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
  • 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 109

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

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 109

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 109

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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