Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I run a disposable e-mail service that accepts all incoming mail and forwards it to a PHP script that stores it in a database for people to view. Before now, I have been on shared hosting with cPanel, which makes it easy to pipe e-mails to a script. Now, however, I got my own VPS, and it doesn't have cPanel. How do I pipe e-mails to script? Further, how do I pipe emails to any address on certain specified domains to my script? You see, aside from the main domain, there are several alternate domains that people can use if the main domain is blocked, and on each domain I want any address to be usable (xyz@domain1, abc@domain2, anythingelse@domain3).

The VPS has Ubuntu 9.04 installed, and I have been experimenting with Postfix, though I can switch to Exim or Sendmail if it is easier.

share|improve this question
up vote 3 down vote accepted

Figured it out at last. I used Sendmail in the end. I turned on the virtusertable feature in, (see the info on the Sendmail Website), then put in /etc/mail/virtusertable the following line: parser@localhost

Which pipes all mail to that domain to the user "parser". Then, I put in at the end of /etc/mail/aliases this line:

parser: "|/path/to/script/parser.php"

After that, I had to run these commands (from /etc/mail):

makemap hash virtusertable.db < virtusertable
/etc/init.d/sendmail reload
/etc/init.d/sendmail restart

That did it!

share|improve this answer

Most MTAs allow you to configure which delivery agent to use (MDA) and I would strongly recommend procmail - it provides a huge range of functionality, e.g. its fairly simple to configure it to filter incoming mail using spamassassin, forward the emails to your PHP script, send out an automatic reply, forward a copy to yourself and write a copy to the mailbox.

Indeed, procmail effectively has its own programming langage - the man pages don't do it justice. There are some books available. Or try Google for articles.

Most distros use procmail as the MDA with some MTAs (notably sendmail). I'd be wary of using Exim (see posts elsewhere). Often, since postfix provides its own MDA, procmail is not configured - but it can be easily added.

Note that the MDA operates on messages after the MTA has decided where to deliver them. If you want to filter messages prior to delivery (e.g. which are being relayed by your MTA) then you should probably look at a milter.

share|improve this answer
I tried Exim, it's really more than I need I think. Looking into procmail, I found… on StackOverflow. Does that code look correct? – D. Strout Feb 5 '11 at 13:28
+1 Procmail is enabled by default in most MTAs these days, you just have to make sure Procmail is installed and create a $HOME/.procmailrc. At least Exim and Postfix usually have this configured already. Procmail offers some clear benefits over directly forwarding to a script of your own, especially when it comes to robust error handling. If your script fails for whatever reason, the email is not entirely lost (and it's easy to put in security mechanisms like a safety copy of each incoming message to a directory in /tmp until you have a good, working, properly tested configuration). – tripleee Jul 25 '13 at 6:47

In Exim this would be handled by a wildcard alias, and a pipe transport. Look for the equivalent for Postfix. Aliases can be configured to specify a command to handle the message.

If you have other domains you want to handle differently, setup virtual domain aliases and put the wildcard in the aliases file for the domain.

It looks like Postfix has a pipe daemon to handle deliveries to a command.

EDIT: Exim solution using split configuration. This requires installing the exim4-daemon-light package. I would suggest you also install the exim4-doc-html package to provide the documentation at http://localhost/doc/exim4-doc-html/html/spec_html/.

Add a file in /etc/exim4/conf.d/router named 380_local-config_program_router. Check the options of generic routers i (chapter 15 and 16 in the specification). Change the script name and domain as required.

# This router runs /usr/bin/your-script for all users in your.domain.
  driver = accept
  require_files = /usr/bin/your-script
  transport = program_transport
  domains = your.domain

Add a file in /etc/exim4/conf.d/transport named 380_local-config_program_transport. Check the options for pipe transport (chapters 24 and 29 in the specification). You will need to set the options which don't have a value specified. Also set the correct command which can have options if required.

  driver = pipe
  command = /usr/bin/your-script
  current_directory = 
  home_directory = 
  user = 
  group = 

I modified the configuration from this post.

Edit2: It looks like you can do it with Postfix as well. A solution using a wildcard address for a virtual domain and a maildrop type indirect delivery should do it.

share|improve this answer
I know it's generally possible, and what approximately I need to do. This doesn't really help. What I need are specific instructions, like "edit this file here with this data, and put your list of domains here separated by x". Thanks anyway. – D. Strout Feb 3 '11 at 19:54
@David The exim documentation includes examples. – BillThor Feb 4 '11 at 5:44
Thanks, this helps a lot more. In 380_local-config_program_router, do I need to separate the domains with a specific character? Thanks a lot. – D. Strout Feb 4 '11 at 15:09
Domains are always separated by a colon : Only list the domains you want to redirect to the program. – BillThor Feb 4 '11 at 15:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.