3

What I'd like to do is set up an address like something@myhost.com which will take whatever email it gets and run the full message as STDIN into a shell script. I can't seem to get this working though. Tried a couple solutions, including this one:

How can I route some emails to a script in Postfix?

But my setup is a bit different. I've got the Postfix/Dovecot/MySQL setup, so all email is handled by Dovecot, and Postfix reads its maps from MySQL. Whatever I add to master.cf doesn't seem to work. SpamAssassin has a line in there and that's working fine. I'm stuck!

EDIT: I really wish people would read carefully before downvoting my question. I am using MySQL for my alias maps, so /etc/aliases does not work, I already tried that of course.

4

Obviously will work Douglas Land's answer, with a little addition.
Suppose you have only localhost.localdomain at $mydestination in main.cf.
Then add a virtual alias :

something@myhost.com -> somelocaluser@localhost.localdomain

In main.cf you will need:

alias_maps=hash:/etc/aliases

Then add to /etc/aliases :

somelocaluser: "|/your/script/here"

newaliases+postfix reload and you are good to go.

  • Doesn't work. I set up a virtual alias foo@myhost.com to point to foo@localhost.localdomain and tried foo@localhost too. It bounces back to me instead of going to the script. – CaptSaltyJack May 13 '13 at 19:15
  • Also, mydestination is blank, and main.cf recommends leaving it blank otherwise it can cause problems. If I set mydestination to my host, I get a warning in the mail.log: warning: do not list domain blahblah.com in BOTH mydestination and virtual_mailbox_domains – CaptSaltyJack May 13 '13 at 19:16
  • Well add localhost.localdomain to mydestination. don't add the main domain. – Sandor Marton May 13 '13 at 21:01
  • Still doesn't work. – CaptSaltyJack May 13 '13 at 21:29
  • Logs? I have same config in multiple places, should work. – Sandor Marton May 13 '13 at 22:11
1

I'd go with Procmail. We use this to parse all sorts of mail out to worker scripts and IRC bots for notifications.

Here is an example of how to handle email. http://linuxgazette.net/issue14/procmail.html

Relevant short example done here for ease:

  1. Create a local user called handler, for example.

  2. Go to handler's home directory and create a file called .procmailrc containing:

:0
| $HOME/bin/my.script

This will pass the entire inbound email to the script $HOME/bin/my.script through STDIN.

Also here is where I got help with Procmail when I was first trying to pass email to a script: How to use procmail to get a message into a variable

  • Link was inline with the text. I've expanded my answer though for ease. – SimonJGreen May 13 '13 at 21:32
  • This doesn't work. Like I said.. local mail delivery is not enabled on this system. This server is purely driven by Dovecot+MySQL for mailbox & alias management. Local user accounts do not register as being able to receive mail just because they exist. – CaptSaltyJack May 13 '13 at 21:44
1

You can pipe an entry to a script via /etc/aliases, RE:

foo: "|/your/script/here"

modify /etc/aliases then run newaliases and reload the postfix process.

0

I would check if sieve supports piping to a script. Dovecot supports sieve scripts.

As alternative you can use "procmail" as MDA between postfix an dovecot. Procmail can pipe to a script.

(that said I wonder why Douglas Land's answer does not work, mysql as backend should not make a different?!)"

  • Simple. In the default setup of postfix, in /etc/main.cf, alias_maps is pointing to hash:/etc/aliases. In my main.cf, that line is gone, replaced with virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf, mysql:/etc/postfix/mysql_virtual_alias_domainaliases_maps.cf. All aliases are handled via a mysql query. I tried using pipe in the DB too, and that didn't work. – CaptSaltyJack May 11 '13 at 21:14
  • aliase_maps and virtual_maps are two different things. You should read "man aliases" and "man virtual" – Tim Haegele May 12 '13 at 19:10
0

Something that hasn't been mentioned is that you can use more than one lookup table (separated by comma or space):

virtual_alias_maps = mysql:/etc/postfix/mysql-virtual.cf
alias_maps = mysql:/etc/postfix/mysql-aliases.cf hash:/etc/aliases

The first one (virtual_alias_maps), will search for an email and return a username (for example: info@example.com -> info1234).

Then, the second one (alias_maps) will search for a username in your database, if not found, will search it at /etc/aliases. This way, you can still manage your aliases in MySQL and set your scripts in /etc/aliases, and no need to append "@localhost.localdomain" to the local users.

To test if your setup is correct, use:

postmap -q info@example.com mysql:/etc/postfix/mysql-virtual.cf
postmap -q info1234 mysql:/etc/postfix/mysql-aliases.cf hash:/etc/aliases

Using Local accounts with aliases in MySQL:

In one of my custom setups, I'm using local accounts for users and MySQL for aliases only. So this is how I set it:

virtual_alias_maps = hash:/etc/postfix/vusers
smtpd_sender_login_maps = hash:/etc/postfix/vusers
virtual_alias_domains = /etc/postfix/vhosts
alias_maps = mysql:/etc/postfix/mysql-aliases.cf hash:/etc/aliases

As you can see, feel free to mix them anyway you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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