I have set up an email id my PHP web application. Users will send emails to this id.
I want to process these emails in the application. Ho do I go about doing this?
Thanks in advance.
I have set up an email id my PHP web application. Users will send emails to this id.
I want to process these emails in the application. Ho do I go about doing this?
Thanks in advance.
You need to implement an email client in Php. This is probably going to be a POP client.
This code would query the POP server containing your email, download it, and then you could parse it as needed.
A quick google search of "POP client php" has revealed a vast array of different options. Its hard to tell if there's really "The One True PHP POP Library", otherwise I'd include it here. If you are using a preexisting framework, you may wish to check to see its level of POP support, otherwise check the google results above and take your pick. Or it may just be easiest (and most educational :) ) to roll your own.
I recently worked on a project that required parsing of email from gmail and updating database with certain values based on the contents of the email. I used the ezcMail library to connect to the mail server and parse the emails.
The strategy I adopted was to filter all interesting incoming mail with a label "unprocessed". Run the PHP script via a crontab every 15 minutes. The script would connect to the mail server and open the IMAP unprocessed folder and parse each email. After inserting the interesting values into the database, the script moves the files to another IMAP folder "Proccessed".
I also found IMAP to be better than POP for this sort of processing.
Use procmail if it is installed on your system. Put these lines in a .procmailrc file in the home directory of the user who receives the e-mail.
:0
| /path/to/your/script.php
Or you can also use a .forward file containing
"|/path/to/your/script.php"
Procmail has the advantage that it allows you to deal with more complicated filtering if your application ever requires it.
Your script.php file will read the headers and body of the e-mail from stdin.
--
bmb