Inbound email with PHP and vpopmail

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.

Win an Annual Membership to Learnable,

SitePoint's Learning Platform

  • http://www.casualcode.com eXplosive

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

  • jorgerpo

    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.

  • Alan Knowles

    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.

  • Ben

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

  • http://www.homeorchardsociety.org SRTech

    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.

  • http://www.rideontwo.com z0s0

    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.

  • http://nathanwwong.com someonewhois

    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.

  • Lach

    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/

  • dusoft

    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…

  • http://www.igeek.info asp_funda

    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!!

  • YES!

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

  • TT
  • email2php.com

    Hello,

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