Solved

Extract email address from returned email

Posted on 2008-10-06
7
710 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 108

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 108

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 108

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 108

Expert Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now