How to configure Postfix to relay all incoming mails to my Python script Without checking if user exists on the system?
Asked Answered
A

1

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 [email protected]. 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.

Acaleph answered 30/5, 2014 at 12:0 Comment(0)
S
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' [email protected]

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

Sundin answered 31/5, 2014 at 3:44 Comment(8)
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 ?Acaleph
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.Sundin
But I want to save as different files. Each mail should correpond to a file.Acaleph
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.Sundin
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.Acaleph
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.Acaleph
Until you fix the script errors, you can set soft_bounce = yes to prevent mails from bouncing to the senders. More about it postfix.org/postconf.5.html#soft_bounceSundin
Tip: the script is executed under user nobody by default - you can change it using main.cf option default_privs.Lepton

© 2022 - 2024 — McMap. All rights reserved.