Inbound email with PHP and vpopmail

By | | PHP

I suspect many of you – like SitePoint – use PHP to process inbound email for various purposes. For example, our warehousing / fulfillment company sends us an email with an XML attachment notifying us when an order has been shipped. It includes various useful details (shipping times, tracking numbers, freight costs, etc) that we need to store in our database.

I’ve hit problems in the past trying to shoehorn this behavior into our vpopmail installation. Vpopmail is a handy set of patches for qmail that ease the handling of large numbers of email domains and users.

The key problem is, vpopmail generally runs as the “vpopmail” user, and delivers all mail while executing under this UID. Hence any scripts it invokes do not have permission to access the PHP libraries that make our applications tick.

A lot of Googleing led me no-where, other than the questionable option of making all our PHP libraries world-readable. No thanks!

I discovered that our best bet was to create a local domain to have the relevant emails delivered to. In a vpopmail installation, local domains behave just like regular qmail domains rather than vpopmail’s “virtual domain” approach.

e.g someuser@yourlocal.domain.com

where “processing.sitepoint.com” is setup as a local domain.

Setting up a local domain is simple; just add it to /var/qmail/control/locals (and don’t forget to put it in rcpthosts, too). Then -HUP qmail-send and you’re good to go.

Once this is done, emails to someuser@yourlocal.domain.com are delivered to the UNIX system account “someuser”. And most importantly it is delivered under that users’ UID/GID.

Thus the script that processes these emails now executes as “someuser” which presumably has access the relevant libraries, and can get the job done.

Written By:

Jules Szemere

A former member of the SitePoint programming team, Jules now wrangles the code behind a variety of web sites and applications. Right now he's working on the Wormly uptime monitoring service.

Website
>> More Posts By Jules Szemere

 

{ 13 comments }

email2php.com January 14, 2008 at 11:44 pm

Hello,

an alternative to the site just mentioned before might be http://www.email2php.com.

TT January 6, 2008 at 12:57 pm
YES! June 16, 2006 at 5:31 am

Thank you, I spent a 3 hours looking at this and boom you have the answer. What a life saver.

asp_funda July 8, 2005 at 4:49 am

Well, as I’m currently in the process of making an application that requires incoming email to be processed by PHP, I had to look around a bit to figure the main part out, ie., getting the mail message, as I’m using PHP’s inbuilt imap functions. Getting the TEXT message wasn’t tough but getting the HTML message from a multi-part email was somewhat sweating!! Still, after it was done, it didn’t feel like any tougher than opening a db connection to MySQL!! :D

As for qmail, I agree, I wouldn’t use it again, Horde is better!!

dusoft July 4, 2005 at 3:58 pm

That article is of no use to users that don’t have access to server and that’s majority. I have been working on php mail processing for a year now – mail comes to catch-all account, then is processed by PHP script (run every hour – enough for what we need) and send to the respective recipients…

Lach July 1, 2005 at 2:42 am

For those wanting a guide on incoming email processing in PHP, this evolt.org article might be of some use: http://www.evolt.org/article/Incoming_Mail_and_PHP/18/27914/

someonewhois June 27, 2005 at 10:48 am

Hmm, I’m not sure how well it works, but for those of you who wish to have an e-mail address dedicated to a PHP script, you can look at Wordpress’ code. ./wp-includes/class-pop3.php is a pop3 class, and ./wp-mail.php is the thing that calls it. I’ve never used it, but it seems to work.

z0s0 June 26, 2005 at 8:10 pm

Alan I have to agree; we will not be using qmail again for inbound mail. Ever! Ben et al, I’ll see what we can do re: getting a longer article on the topic written.

SRTech June 25, 2005 at 8:56 pm

Actually a longer article on how to process inbound mail would be great!

I would be interested as well. Maybe with some information on using cron and php to proccess from a pop account for those of us without our own servers.

Ben June 25, 2005 at 4:43 am

Actually a longer article on how to process inbound mail would be great!

Alan Knowles June 24, 2005 at 10:51 pm

Qmail’s only real use is for outbound email if you send out huge amounts of email, I would never put it anywhere near an inbound server..
install exim – stuff like that is a one line in the configuration script.

jorgerpo June 24, 2005 at 11:07 am

wow!
what a simple solution.

I spent like 3 weeks a year ago trying to solve this very same problem. We tried a lot of ideas, and finally we went to use Horde and IMP, without using our own implementation.

eXplosive June 24, 2005 at 10:47 am

Would you mind linking to some more resource? I have never use inbound mail with PHP before.

Comments on this entry are closed.