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@email.com>

- To: email@email.com

Link to comment
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
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.