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.
Receiving mail with PHP
Moderator: General Moderators
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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:
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.
Code: Select all
$fp = fopen("php://stdin", r);
$email = fread($fp, 100000);
fclose($fp);Receiving mail with PHP
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?
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?
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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.
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.