57

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Niyaz
  • 53,943
  • 55
  • 151
  • 182

11 Answers11

53

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.

Shoan
  • 4,003
  • 1
  • 26
  • 29
  • @Shoan i know this answer is really old.... but would you mind posting some code for me? – pattyd May 26 '13 at 19:59
  • 1
    @pattyd Unfortunately, I don't have that code anymore. But it should relatively simple to do it with my answer in the outline. The docs for the Zeta component will show you how to read the email. – Shoan May 30 '13 at 16:29
  • 1
    @pattyd This isn't how you ask for things on StackOverflow. The `Ask Question` button is... – Bojangles May 30 '13 at 16:29
  • 4
    @Bojangles, I was just checking... Even if i used the mythical "ASK QUESTION" button, you would probably mark it as a duplicate! – pattyd May 30 '13 at 21:05
  • 2
    @pattyd Probably not, providing your question was more than "post some code please" instead of "here's my attempt, where did I go wrong?" – Bojangles May 31 '13 at 07:11
17

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 ____@yourapp.appspotmail.com. 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 user_id@mydomain.tld (an email address that doesn't actually exist)
  • mydomain.tld has a catchall email address that forwards to inbox@GAEapp.appspotmail.com
  • 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 ___@emailization.com 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!

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Capitao
  • 285
  • 3
  • 3
14

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
  • 6,058
  • 2
  • 37
  • 58
  • And, there is a great tutorial outlining the above-mentioned process here: http://www.evolt.org/incoming_mail_and_php – Vern Jensen Nov 26 '13 at 20:26
7

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.

ToughPal
  • 2,231
  • 5
  • 26
  • 30
6

I suggest using Zend_Mail component of Zend Framework.

farzad
  • 8,775
  • 6
  • 32
  • 41
4

There is a great library: Try this: http://code.google.com/p/php-imap

barbushin
  • 5,165
  • 5
  • 37
  • 43
3

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.

Doug T.
  • 64,223
  • 27
  • 138
  • 202
2

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.

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
Travis Austin
  • 439
  • 5
  • 14
2

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)

Community
  • 1
  • 1
Flolagale
  • 1,300
  • 11
  • 7
1

There is a great tutorial for this here:

http://www.evolt.org/incoming_mail_and_php

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.

Vern Jensen
  • 3,449
  • 6
  • 42
  • 59
0

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.
http://www.adkap.com/autoresponder.html

justinb
  • 121
  • 6