The Little Guy Posted January 20, 2010 Share Posted January 20, 2010 I am reading an email, I have this: $lines = explode("\n", $this->email); foreach($lines as $line){ if(preg_match('~^to:.+?[\<](.+?)[\>]$~i', $line, $matches)){ $this->to = trim($matches[1]); } } What I would like it to do, is find a "to" line in the header, and get who it is to. My problem is that some emails have 2 (maybe more) "to" lines, I want to extract just the email address, how can I do that? Some values: - To: First Last <email@email.com> - To: email@email.com Quote Link to comment https://forums.phpfreaks.com/topic/189220-get-who-email-is-to/ Share on other sites More sharing options...
cags Posted January 21, 2010 Share Posted January 21, 2010 Something along the following lines should do the trick... $pattern = '#^To:([a-z ]*)? <?([a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4})>?$#i'; NB(s): I capture the name meaning e-mail would be in $matches[2] if you don't need this you can remove the capture group. The pattern for matching an e-mail is rudimentary and can be improved on, technically speaking it won't work on all 'valid' e-mail addresses, but it will work on a high percentage. Quote Link to comment https://forums.phpfreaks.com/topic/189220-get-who-email-is-to/#findComment-999280 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.