Jump to content

[SOLVED] Get email address out of a passage of text


c_shelswell

Recommended Posts

Hi,

 

I'm trying to make a page that deals with incoming mails that have failed. I'm struggling to find anything in the php IMAP functions that will get the intended recipients email address from an array. The closest i've got is

 

Return-path: <> Envelope-to: chris@downloadhome.co.uk Delivery-date: Fri, 18 May 2007 12:15:25 +0100 Received: from mailnull by host.downloadhome.co.uk with local (Exim 4.63) id 1Hp0Qf-0002hC-MT for chris@downloadhome.co.uk; Fri, 18 May 2007 12:15:25 +0100 X-Failed-Recipients: askdjfskljfd@askljfdaskljfaskl.com Auto-Submitted: auto-replied From: Mail Delivery System To: chris@downloadhome.co.uk Subject: Mail delivery failed: returning message to sender Message-Id: Date: Fri, 18 May 2007 12:15:25 +0100

 

the address i need to extract is the "askdjfskljfd@askljfdaskljfaskl.com" does anyone know a way to do this or is there a imap function that'll do it?

 

Cheers

Link to comment
Share on other sites

<pre>
<?php

$string = <<<STR
Return-path: <> Envelope-to: chris@downloadhome.co.uk Delivery-date: Fri, 18 May 2007 12:15:25 +0100 Received: from mailnull by host.downloadhome.co.uk with local (Exim 4.63) id 1Hp0Qf-0002hC-MT for chris@downloadhome.co.uk; Fri, 18 May 2007 12:15:25 +0100 X-Failed-Recipients: askdjfskljfd@askljfdaskljfaskl.com Auto-Submitted: auto-replied From: Mail Delivery System To: chris@downloadhome.co.uk Subject: Mail delivery failed: returning message to sender Message-Id: Date: Fri, 18 May 2007 12:15:25 +0100
STR;

preg_match('/(?<=X-Failed-Recipients: )(\S+)/', $string, $matches);
print_r($matches);

?>
</pre>

Link to comment
Share on other sites

(?<= ... ) is a lookaround, and more specifically, a positive lookbehind. And it does just that: it looks behind to make sure it can match "X-Failed-Recipients: ". If it can, it continues to the next part of the pattern, (\S+), which captures (via the parens) one or more (+) non-whitespace characters (\S).
Link to comment
Share on other sites

sorry i've just realised, and now i'm really stuck. The mails i'd been testing were only going to one recipient. I've just tested with multiple addresses and now need to get more than one address out of the text

 

ie:

Return-path: <> Envelope-to: chris@downloadhome.co.uk Delivery-date: Fri, 18 May 2007 18:01:06 +0100 
Received: from mailnull by host.downloadhome.co.uk with local (Exim 4.63) 
id 1Hp5pB-0003FN-Vo for chris@downloadhome.co.uk; Fri, 18 

May 2007 18:01:06 +0100 X-Failed-Recipients:asdfasfdasdfasfd@pppasdfaf.com, zzzzxxxxzzxzzx@qwqwqwrrrr.com

Auto-Submitted: auto-replied From: Mail Delivery System To:
chris@downloadhome.co.uk Subject: Mail delivery failed: returning message to sender
Message-Id: Date: Fri, 18 May 2007 18:01:05 +0100

 

would there be anyway of adding a comma to the preg match : "preg_match('/(?<=X-Failed-Recipients: )(\S+)/', $header, $eAddress);" in order to get all the email addresses out of the text?

 

Sorry to be so useless - regex really confuses me  :-[

Link to comment
Share on other sites

Assuming "Auto-Submitted" always follows this portion...

 

<pre>
<?php

$string = <<<STR
Return-path: <> Envelope-to: chris@downloadhome.co.uk Delivery-date: Fri, 18 May 2007 18:01:06 +0100 
Received: from mailnull by host.downloadhome.co.uk with local (Exim 4.63) 
id 1Hp5pB-0003FN-Vo for chris@downloadhome.co.uk; Fri, 18 

May 2007 18:01:06 +0100 X-Failed-Recipients:asdfasfdasdfasfd@pppasdfaf.com, zzzzxxxxzzxzzx@qwqwqwrrrr.com

Auto-Submitted: auto-replied From: Mail Delivery System To:
chris@downloadhome.co.uk Subject: Mail delivery failed: returning message to sender
Message-Id: Date: Fri, 18 May 2007 18:01:05 +0100
STR;

preg_match('/(?<=X-Failed-Recipients:)\s*((??!\s+Auto-Submitted:).)+)/', $string, $matches);
print_r($matches);

?>
</pre>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.