Jump to content

Get who email is to


The Little Guy

Recommended Posts

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 protected]>

- To: [email protected]

Link to comment
https://forums.phpfreaks.com/topic/189220-get-who-email-is-to/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/189220-get-who-email-is-to/#findComment-999280
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.