0

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.

Ashley Strout
  • 218
  • 2
  • 13

3 Answers3

4

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

@disposaldomain.net 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
newaliases
/etc/init.d/sendmail reload
/etc/init.d/sendmail restart

That did it!

Ashley Strout
  • 218
  • 2
  • 13
1

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

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

BillThor
  • 27,737
  • 3
  • 37
  • 69
  • 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. – Ashley Strout Feb 03 '11 at 19:54
  • @David The exim documentation includes examples. – BillThor Feb 04 '11 at 05: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. – Ashley Strout Feb 04 '11 at 15:09
  • Domains are always separated by a colon `example.com : example.net`. Only list the domains you want to redirect to the program. – BillThor Feb 04 '11 at 15:45
1

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.

symcbean
  • 21,009
  • 1
  • 31
  • 52
  • I tried Exim, it's really more than I need I think. Looking into procmail, I found http://stackoverflow.com/questions/557906/want-procmail-to-run-a-custom-python-script-everytime-a-new-mail-shows-up on StackOverflow. Does that code look correct? – Ashley Strout Feb 05 '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 06:47