c_shelswell Posted May 18, 2007 Share Posted May 18, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted May 18, 2007 Share Posted May 18, 2007 <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> Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted May 18, 2007 Author Share Posted May 18, 2007 Brilliant thanks effigy. Could you tell me roughly how it works. Does it just look for "<=X-Failed-Recipients:" then stop at the space at the end? thanks Quote Link to comment Share on other sites More sharing options...
effigy Posted May 18, 2007 Share Posted May 18, 2007 (?<= ... ) 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). Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted May 18, 2007 Author Share Posted May 18, 2007 great - thanks again for your help Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted May 18, 2007 Author Share Posted May 18, 2007 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 Quote Link to comment Share on other sites More sharing options...
effigy Posted May 18, 2007 Share Posted May 18, 2007 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> Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted May 18, 2007 Author Share Posted May 18, 2007 thanks i've been trawling regex sites to try and figure this out. Think it's about time i got a book and learnt!!! Cheers Quote Link to comment Share on other sites More sharing options...
effigy Posted May 18, 2007 Share Posted May 18, 2007 By the way, we have a Regular Expressions forum which includes some reference material and examples. I'm moving your topic there. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.