Capture and Manage Email Data with PHP
I'm looking for a way to capture and manage email data using PHP. Basically, what I want to do is capture all the data in an email and then manipulate this data to my specification.
For example, say, I send an email containing a .zip file attachment to myemail@myproject.com, I want to be able to:
- Get the attachment and place it in a specific folder on my site
- Get the text content of the email
- Get the subject of the email
- Get the sender's info i.e. email address
Anyone know how I can get this done efficiently with PHP. I'm using LAMP by the 开发者_C百科way.
Thanks.
Start with PEAR Mail_mimeDecode
. What you are looking to do is ambitious but can be done.
Basically what you will be doing is:
Instructing your MTA to deliver mail from an address to a pipe into your PHP script. Postfix and Sendmail can handle this with an alias like:
myemail: "|/path/to/your/parsingscript.php"
- Parsing out the parts of the MIME email message
- Locating and storing attachments after decoding them from base64 (or other encoding)
- Parsing the headers.
Your PHP script will likely read the email message from STDIN and then pass the string to mimeDecode
, which creates an object containing all the MIME parts.
Assuming your message was received into $str
from STDIN, something like this gets you started:
$mime = Mail_mimeDecode::decode(array('include_bodies'=>TRUE, 'decode_headers'=>TRUE, 'decode_bodies'=>TRUE, 'input'=>$str));
// get the recipient To address:
$to = $mime->headers['to'];
精彩评论