2

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 [email protected], I want to be able to:

  1. Get the attachment and place it in a specific folder on my site
  2. Get the text content of the email
  3. Get the subject of the email
  4. 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 way.

Thanks.

2
  • Adding to Michael's answer, I created a wrapper class for PEAR's Mail_mimeDecode which I hope could help sudocode.net/sources/includes/class-email-php Commented Aug 11, 2011 at 16:14
  • Thanks a lot. This will definitely come in handy. Can you give me some pointers as to how I can setup SendMail piping in Linux, specifically Ubuntu? Commented Aug 12, 2011 at 22:45

1 Answer 1

2

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']; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It looks a tad complicated though. I'm pretty good with PHP, but I'm quite the novice with Linux, especially server administration. Any idea where I can get more information how to setup piping for sendmail on Ubuntu? And is using IMAP functionality a more viable option?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.