Solved

Extract email address from returned email

Posted on 2008-10-06
7
737 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Move wordpress site 3 41
Seeking info on how to integrate RETS feed based MLS data into a WordPress site 5 49
PHP Syntax Error 4 27
PHP Query return divisible by 3 3 18
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
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…

832 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