Reputation: 1441
I have a PHP script which has the source of an email. I aim to split the headers into variables $To
and $From
The issue comes when trying to split the to and from strings up. What I need the script to do is take
From: John <[email protected]>
To: Susy <[email protected]>, Steven <[email protected]>, Mary <[email protected]>
and return only the from address and the to addresses which are on my site. I.e.
$From = '[email protected]';
$To = array('[email protected]', '[email protected]');
So the code needs to turn a string of email addresses into an array and then filter out the ones from other sites. It's the first part that is proving difficult because of the different ways an email address can be listed in a header.
Upvotes: 2
Views: 2133
Reputation: 48721
preg_match_all("/<([^><]+)/", $headers, $matches);
print_r($matches[1]);
Output:
Array
(
[0] => [email protected]
[1] => [email protected]
[2] => [email protected]
[3] => [email protected]
)
The first one is always From email address.
Upvotes: 1
Reputation: 197787
Edit
As you've now specified that you have the headers as a string but you actually need to parse the addresses from it, there is no need to reinvent the wheel:
These two functions will do the job for you, the last one will give you an array with objects that have the email addresses pre-parsed, so you can easily take decisions based on the host.
It was not specifically clear to me what your actual problem is from your question.
As long as you are concerned about filtering a string containing one email address (cast it to array) or an array containing one or multiple addresses:
To filter the existing array of email-addresses you can use a simple array mapping function that will set any email that is not matching your site's host to FALSE
and then filter the array copy Demo:
$addresses = array(
'[email protected]',
'[email protected]',
);
$myhost = 'mysite.com';
$filtered = array_map(function($email) use ($myhost) {
$host = '@'.$myhost;
$is = substr($email, -strlen($host)) === $host;
return $is ? $email : FALSE;
}, $addresses);
$filtered = array_filter($filtered);
print_r($filtered);
This codes makes the assumption that you have the email addresses already gathered. You have not specified how you parse the headers already in your question, so it's actually unknown with which data you are dealing, so I opted to start from the end of your problem. Let us know if you have more information available.
Upvotes: 2
Reputation: 27855
<?php
$k= "......Subject: Write the program any of your favorite language whenever if you feel
you are free
From: Vinay Kumar <[email protected]>
To: [email protected], [email protected],Susy <[email protected]>, Steven <[email protected]>, Mary <[email protected]>
Content-Type: multipart/alternative; boundary=bcaec53964ec5eed2604acd0e09a
--bcaec53964ec5eed2604acd0e09a
Content-Type: text/plain; charset=ISO-8859-1
.......";
if(preg_match('/From:(?P<text>.+)\r\n/', $k, $matches1))
{
if(preg_match('/(?P<from>([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .'(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+)/', $matches1['text'],$sender ))
{
print_r($sender['from']);
}
}
if(preg_match('/To:(?P<text>.+)\r\n/', $k, $matches2))
{
if(preg_match_all('/(?P<to>([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' .
'(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+)/', $matches2['text'], $reciever))
{
if(isset($reciever['to']))
{
print_r($reciever['to']);
}
}
}
to get the subject:
if(preg_match('/Subject:(?P<subject>.+)\r\n/', $k, $subject))
{
print_r($subject['subject']);
}
Upvotes: 1