Blog Post RSS ?

Blogs » PHP » Inbound email with PHP and vpopmail
 

Inbound email with PHP and vpopmail


  • Save to
    Del.icio.us

by Jules Szemere

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.

This post has 13 responses so far

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

     
  2. 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.

     
  3. 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.

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

     
  5. 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.

     
  6. 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.

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

     
  8. 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/

     
  9. 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…

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

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

     
  12. I would like to invite you all to try out Email2HTTP to process incoming email in PHP.

    Feel free to email me through the contact link on the Email2HTTP website if you run into any trouble, I would be more than happy to help you with it.

     
  13. Hello,

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

     

Sponsored Links

Leave a response

You are not logged in, log in with your SitePoint Forum username and password.

-OR- Post Anonymously

* Make sure any code samples are escaped (i.e. ‘<b>’ becomes ‘&lt;b&gt;’).

If not logged in, your comments will be placed in a moderation queue. This means your comment may not appear until one of our moderators approves it.

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.

Logo Design, Web page Design and more!

99designs

  • Custom logo designs created ‘just for you’.
  • Pick the design you like best.
  • Only pay if you’re satisfied with the result.

Want More Traffic?

Get up to five quotes from qualified SEO specialists, with no obligation!

Get A Free SEO Quote Now!