Receiving mail with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danny
Forum Newbie
Posts: 3
Joined: Sat Jun 05, 2004 8:28 am

Receiving mail with PHP

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

If you host has PEAR installed/available then you might want to try one of the PEAR Mail packages.
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
danny
Forum Newbie
Posts: 3
Joined: Sat Jun 05, 2004 8:28 am

Receiving mail with PHP

Post 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?
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
danny
Forum Newbie
Posts: 3
Joined: Sat Jun 05, 2004 8:28 am

Post by danny »

Thanks for your help on this. It has given me another point to start at (again!!)
Post Reply