Php Problem Overview
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.
Solution 1 - Php
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 (now) Zeta Components 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.
Solution 2 - Php
Recently I wanted to be able to receive emails immediately in something I was making so I did some research (I came looking on this question here too actually) and I ended up finding Google App Engine to be pretty helpful. It has an api you can use to receive and process emails sent to
[email protected]. I know that it doesn't really seem helpful since you probably don't want your app on App Engine and you want to receive emails at yourdomain.tld, but with a little setup you can get what you want.
My basic setup is like this:
- User sends email to [email protected] (an email address that doesn't actually exist)
- mydomain.tld has a catchall email address that forwards to [email protected]
- GAEapp (a tiny app on app engine) receives the email, processes it out, and sends a post request with relevant stuff to mydomain.tld
So basically you can make a little GAE app that works like a go between to grab the emails. Even with the redirect it'll work out ok, the email will be fine.
Also I decided to learn me some django and I made a free app called Emailization that will basically do that for you. You create a recipient like
[email protected] and give a URL to POST to. Anything sent to that address gets POSTed to you URL. You can make a catchall on your domain that forwards to that emailization recipient and you'll get email through the catchall too!
or you can see a small GAE app I made that you can setup yourself that does the same thing.
Hope that helps somebody!
Solution 3 - Php
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.
Or you can also use a .forward file containing
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.
Solution 4 - Php
Check out fMailbox. It does not require any non-standard extensions (such as imap) and has been tested with various servers, attachments, multipart messages, SSL, and more.
Solution 5 - Php
I suggest using Zend_Mail component of Zend Framework.
Solution 6 - Php
There is a great library: Try this: http://code.google.com/p/php-imap
Solution 7 - Php
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.
Solution 8 - Php
There are a number of hosted solutions that will accept email for your domain and then post it a script on your website. Most of these will handle the parsing of the messages for you (separating the attachments, "to" "from" and other addresses, etc).
You just create a script that receives a FORM POST and does whatever you need with it.
You can also look at Mandrill (by MailChimp), SendGrid, and PostMarkApp.
Solution 9 - Php
There is a great tutorial for this here:
which covers how to have the emails forwarded directly to your script, which your script reads via stdin (fopen, fread, etc.) The tutorial code even does basic parsing of the header/body for you.
Solution 10 - Php
Hosted solutions as Travis Austin suggested work well.
If you are looking for a self-hosted one, you can have a look at the Mailin module allows you to receive emails, parse them and post them to a webhook of your choice.It also checks the dkim and spf, computes a spamassassin score and determines the message language.
I don't know if it will suit your needs since it is written in node.js, but the more options you have, the better. (Disclaimer: I am the maintainer of Mailin)
Solution 11 - Php
If you want to avoid reaching out over POP or IMAP to another server to pull-down the email, you can add a 'hook' into the email receive process on some SMTP server you set up (possibly the same php server). Then just have the destination email handled by this server.
Here is an example with postfix, but similar things are possible with sendmail as well.