Reputation: 805
I need to parse email body(it can be both text and html), of forwarded email, to find original sender of email(both name and email).
So lets say that John send email to Steve, and Steve forward to Matt. So, in most clients message will look like:
From: John Smith [mailto:[email protected]]
Sent: 02 March 2012 AM 01:23
To: 'Steve'
Subject: Hello Steve
Or
---------- Forwarded message ----------
From: John Smith <[email protected]>
Date: 02 March 2012 AM 01:23
Subject: Hello Steve
To: Steve <[email protected]>
So, how can I extract from name and from email values in those two cases? Please note that I need to extract only first appearance of this text, because there can be multiple appearances of lines "From:...".
Upvotes: 1
Views: 2209
Reputation: 43673
With regex
/^From:\s+(.*?)\s*(?:\[mailto:|<)(.*?)(?:[\]>])$/
you will get first match name and second match email address
<?php
$string = "...";
$pattern = '/^From:\s+(.*?)\s*(?:\[mailto:|<)(.*?)(?:[\]>])$/mi';
preg_match($pattern, $string, $matches);
print_r($matches);
?>
Sometimes there is also '
or "
at the begining and end of the name contact. To eliminate that, use:
/^From:\s+["']?(.*?)["']?\s*(?:\[mailto:|<)(.*?)(?:[\]>])$/
Upvotes: 3