How to rewrite outgoing address in Postfix
Sometimes I find myself configuring an internal Linux machine to be able to send emails for alerts or from a particular application. Since this will not be a primary mail server, I just want to rewrite the outgoing address to be something that make sense with the proper domain for the users. Here are the quick steps to accomplish this:
vi /etc/postfix/main.cf
Modify the "mydomain" variable to your email domain
mydomain = example.com
Make sure to uncomment the proper network interface. I'm usually lazy and just do all.
inet_interfaces = all
Now at the bottom of this file you need to specify the generic map file for rewriting the address.
smtp_generic_maps = hash:/etc/postfix/generic
Save and exit main.cf. Now we need to edit /etc/postfix/generic
vi /etc/postfix/generic
You need to specify the mapping of the original address to the one you want. Since in this case I just want to rewrite everything I usually add the following two lines and it seems to catch everything.
root@example.com no-reply@example.com @example.com no-reply@example.com
Save and exit the file. Now we need to create the postfix db.
postmap /etc/postfix/generic
This will create the /etc/postfix/generic.db hash file. Now just restart postfix and test.
/etc/init.d/postfix restart
Comments (3)
I included both mostly to show how to do it by specific user or do by the entire domain. So in this case if I originally setup a root cron job to send an email, the 'root@example.com' will catch it. But if later in the future another user on the machine sets up a cron job to email and doesn't explicitly specify a user in the postfix generic DB, it will still send out the email with a valid domain.
Note that the '@example.com' has the lowest precedence and it will only be matched if a more specific match cannot be found.
Hope that helps!
sorry - too quick, 2 things I shoudl mention re my previous comment:
1. I don't understand why root@example is needes if @example is a "catch all" for all items
2. thanks it's very helpful of you to put this code up
root@example works fine
@example doesn't
why do you need both?