6

I want to configure my Postfix server as accepting all incoming mails to any arbitrary users who don't have to exist on the system, say example@mydomain.com. Now Postfix says, User unknown in local recipient table. What I want is to accept this e-mail without rejecting it and pipe it to my python script. Any help would be gladly appreciated.

Carlos
  • 177
  • 3
  • 8

1 Answers1

6

You can use

luser_relay - Optional catch-all destination for unknown `local(8)` recipients. 

Add the following to your main.cf.

#/etc/postfix/main.cf
#...
#...
mydestination = $myhostname, localhost.$mydomain, localhost, mydomain.com
local_recipient_maps =
luser_relay = catchall
alias_maps = hash:/etc/aliases
#...
#...

and the following to your aliases file.

#/etc/aliases
catchall:  |/path/to/your/python_script.py

Run the following commands

postalias /etc/aliases
service postfix reload

and you can test the setup by following command

echo "test email"|mail -s 'Test email' unknown@mydomain.com

Emails to unknown users will be delivered to the python script. Hope that helps.

clement
  • 2,982
  • 2
  • 14
  • 11
  • Thank you very much @clement. By the way, can I somehow store that mail as file onto disk without any external script usage. I want to do it with only Postfix configuration. Is it possible ? – Carlos May 31 '14 at 08:48
  • 1
    Ya just change the alias file to `catchall: /path/to/file` and run `postalias /etc/aliases`. All incoming mails will be appended to the file. – clement May 31 '14 at 09:26
  • But I want to save as different files. Each mail should correpond to a file. – Carlos May 31 '14 at 11:21
  • 1
    Then redirect mails of `catchall` (`catchall: someuser`) to an UNIX user and configure postfix to perform `Maildir` delivery (`home_mailbox = Maildir/`). Every mail will be delivered to a separate file in `/home/someuser/Maildir/`. Or you can write a small python script to write every incoming mail to a separate file. – clement May 31 '14 at 11:30
  • Thanks mate. I think it is a good idea to write a python script. Because you mentioned that Postfix will write each mail to user's directory however as I said above those users dont need to exist in the system. – Carlos May 31 '14 at 11:33
  • When my script gets error, Postfix directly send error information to message sender even about the line number of script where error occured. I dont want sender to know error of my script. How can I prevent Postfix from sending this error. Thanks. – Carlos Jun 02 '14 at 16:48
  • 1
    Until you fix the script errors, you can set `soft_bounce = yes` to prevent mails from bouncing to the senders. More about it http://www.postfix.org/postconf.5.html#soft_bounce – clement Jun 03 '14 at 07:25
  • Tip: the script is executed under user nobody by default - you can change it using main.cf option default_privs. – Messa Jan 03 '19 at 15:51