I need a solution like this one where my mailserver picks up certain e-mail accounts and forwards them to a script that rips them apart and performs certain functions based on the content. However, that tutorial's solution requires PHP being compiled as a CGI binary whereas mine is an Apache module. I contacted the author of the article and he confirmed there still no way around that. One alternative he mentioned is to have pop web email and use the script on those emails. My question is, have any of you worked with incoming email handling via PHP before? What are your experiences with it? And what about pop web mail on Apache module PHP? Any products you can refer me to? My servers come with Sendmail, PostFix and Qmail. Which one can you recommend looking into first?
What you could do is have your mail servers forward all email to a certain address and write a script that queries that POP3 account regularly via a cron job. It's possible and quite straightforward if you have PHP imap_*() functions enabled. An issue I ran into is that a whole lot of web hosts, including popular ones like Pair, don't compile their PHP with --with-imap, since it could hang the httpd (which makes sense, as one thread could be used for minutes continuously if the remote host is not accessible or there's lots of email to be fetched). If PHP doesn't work, you can use a Perl script to fetch the email and dump everything into a text file or database, since Perl and the necessary modules are most likely present on your server. This solution is a bit sloppy but works in all server configurations. Let me know if you need further help.
Thanks Gmorkster. Didn't think of Perl as an alternative. Here's my relevant imap details form a phpinfo(): Here's the scenario I want to tackle: A 3d party service provider send automatic e-mails based on certain events to do with the service. Based on the contents I want to do a db operation. The emails come in to so I need to set it up to take that one only and send it to a PHP script which will do the business. So you reckon I can set up a pop3 account and call imap functions with a cron job? I'm a novice to all that, would you know a place for me to read up on it all? Does it involve some scripts or modules ot be installed or is pop3 exactly what Qmail, Postfix and SendMail offer?
Looks like your server comes with everything you need to make it work. And yes, you can write a script that queries the POP3 account and performs specific operations based on the content and make it run every x minutes via a cron job. The script should be extremely straightforward, you need just a few lines of code for connecting and fetching the email. The PHP imap function reference you can find at http://php.net/imap -- also look at the examples and user comments that come with each function. I believe I've seen some classes on phpclasses.org that wrap around these functions, you might find one of those easier to use. Not quite sure what you mean by that-- you can set up the POP3 account with whatever your server comes with, no extra fuss.
My server comes with Qmail, PostFix and SendMail. That last question of mine was whether them 3 mailserver whateveryouwannacallits are all I need to set up POP3 then? Thanks a bunch for clearing all this up btw, I'll study the imap funcitons and dig out my phpclasses.org login details to have a browse.
Qmail claims to be the one most used world wide. Would you agree I'd focus my research on that one? I don't fancy playing around with all 3 to be honest, would rather elimate one or two right away.
Alas, though the rest of Gmorkster's advice was excellent, I have to contradict one point: MTAs (mail transport agents), such as the three mentioned applications, work with SMTP, and don't concern themselves at all with POP3, IMAP and so forth. (Qmail is almost an exception, since it's got a closely coupled POP3 daemon). Setting up POP3 or IMAP on a Unix server is an entirely different operation, and doesn't involve the MTA except to agree on a standard storage format (usually either mailbox or maildir, the latter being the more modern). What you need to do is install and configure a POP3 daemon in addition to the MTA; I recommend using Courier's POP3 daemon, since it comes standard on most Linux distros and supports the maildir storage format out of the box. As for the actual MTA, I've used all the different popular options over the years, and Exim would definitely be my number-one choice now. The second place goes to Postfix, which is excellent as well. Qmail is decent but somewhat quirky (but does have that tightly coupled POP3 daemon going for it...), and Sendmail I'd stay away from at all costs (you will too, after you've had a go at configuring it ). If you need any help setting all this up, drop me a PM
Excellent advice Willy and I agree on all of it. I personally would go with a Courier/Qmail combination, but that is only because I know it so much better than the rest.
Thanks Willy & Blinx! I found out a bit more about PHP's imap functionality and found some classes that wrap up all the functionality nicely. I'll check-out the mentioned POP3 packages. Thanks a bunch!
This seems like a thread that is more than 3 years old but i'm facing the exact same issue. I have PHP compiled as an Apache module, not a CGI binary. I'm running cygwin with exim. Exim works to send email. However, what I am trying to accomplish is Incoming PHP mail. I want to be able to send all incoming mail...abc@myserver.com to a script, which will then parse the email and store it in a database. I already went through the steps mentioned in the evolt incoming php mail tutorial (for my exim installation, but with no success.) I dont know how to proceed. If anyone knows how to do this, please let me know.
Hello upshut, as I have just come across your message: Try consider our service at www.email2php.com It saves you the effort of puzzling about the correct server configuration. We transform every received email into a http request that calls one of your specified scripts, with the email content delivered in one request variable. Should be straightforward with this to get your things going. Thomas email2php.com