Page 1 of 1

Receiving mail with PHP

Posted: Sat Jun 05, 2004 8:28 am
by danny
I am trying to create a setup where when I send an e-mail to a specific address, a PHP script runs, and parses the e-mail. This should then insert the message, subject, etc into a database then save the attachments into a directory on the webserver.

The web host has already set up the e-mail address to forward to a PHP script, and this works fine. When an e-mail is sent, the script runs. The script though is not a shell script, as the web host does not allow this.

My understanding is that i need the IMAP functions to get the e-mail, parse it, and put a new row into a database with the headers, message, and a link to any attachments, as well as putting the attachment into a folder. The web host has set it up so that the first 500k of the e-mail, and send it to the script via HTTP_POST

This is just about where my understanding of the IMAP finishes. I have tried many scripts, and looked at many tutorials, but cannot get out of them what I need.

If someone could help, it would be much appreciated.

Thanks in advance
Daniel.

Posted: Sat Jun 05, 2004 8:38 pm
by markl999
If you host has PEAR installed/available then you might want to try one of the PEAR Mail packages.

Posted: Sat Jun 05, 2004 9:10 pm
by launchcode
Danny - if your host has set-up the mail to forward directly to your PHP script, you don't need to mess with IMAP, etc - the email text would be available directly via STDIN - the follow piece of code should help:

Code: Select all

$fp = fopen("php://stdin", r);
   $email = fread($fp, 100000);
   fclose($fp);
It only reads in 100,000 bytes worth because that is what I needed it to do - but you can tailor this (eof, etc) - basically the entire email, header and all now sites in the $email string ready for you to do whatever you need with it.

Receiving mail with PHP

Posted: Mon Jun 07, 2004 1:10 am
by danny
In response to Mark's comment, I don't believe the host has PEAR installed. When I look at the php_info() it has been setup '--without-pear' in the PHP commands section. I'm not sure if the host will set PEAR up yet, and won't be able to find out for another few days. (no-one to respond to the e-mails for a few days.)

launchcode, with the mail, it is being sent to the php script with the http_post variable, $text. The first 500kb of the e-mail is sent in this variable.

This still leaves the question of how i parse the e-mail. Is there special PHP commands that someone knows of that will parse the message, getting the headers, message and attachments, etc?

Posted: Mon Jun 07, 2004 6:00 am
by launchcode
There is no command / function to do this for you. The headers of emails are relatively standard and there are key things you can look out for and extract with simple string functions - it's just some badly written mail clients might break it.

Re: PEAR - it doesn't matter if PHP was compiled without Pear support, you can still just download and use the libraries from the Pear web site - they're only PHP files after all.

Posted: Mon Jun 07, 2004 9:29 am
by danny
Thanks for your help on this. It has given me another point to start at (again!!)