Masquerading

----------

Postfix - relayhost configuration, separate for each domain

There was a need to configure sending mail from one postfix mail server through another. Moreover, the server had several domains, each needed its own external mail server for sending mail.

In general, the problem of mail forwarding is solved very simply. If you want all mail to be sent through another smtp server, just enter the address of this server in the postfix parameter.

relayhost = mailsrv.mymail.ru:25

After that, all outgoing mail will be sent to the mailsrv.mymail.ru server. This option did not suit me. I wanted to test  proxmox mail gateway on a production server, but only on one domain. To do this, I had to send mail from one specific domain to be sent through a new mail gateway.

For some reason, I found a lot of advice on the Internet to configure mail forwarding through transport_maps. I tested this parameter, but it is not for my case. Using transport_maps, you can forward all incoming mail to some other server. I needed not to forward the incoming, but to configure the sending of messages, that is, outgoing mail.

To do what I need, I need a parameter -  sender_dependent_relayhost_maps . Add it to the config as follows:

sender_dependent_relayhost_maps = hash: / etc / postfix / relayhost_map

The contents of the relayhost_map file.

@ prox.mymail.ru 10.1.3.15:26

Create an indexed database from this file:

# postmap hash: / etc / postfix / relayhost_map

After that, you can restart postfix and check. In this case, I sent all the mail from the prox.mymail.ru domain through the external smtp server 10.1.3.15 to port 26. It uses proxmox mail gateway to forward mail. All other domains both sent mail themselves locally and will send.

How to change the subject of the letter and sender address through postfix

I want to share a couple of useful tips on setting up a mail server. I will tell you how to automatically change the subject of all messages sent via postfix, as well as to replace the sender address. The other day in the comments on one article about sending mail, the reader showed a simple and interesting solution, which I decided to check, supplement and arrange as a separate article

Replacing the sender address can be useful in different situations. I will use it specifically to solve the problem of sending letters through Yandex mail servers. For successful sending of a letter, Yandex requires that the sender address in the letter coincides with the address for authorization on the server. If this is not done, then you will receive an error during sending -  Sender address rejected: not owned by auth user .

May 8 15:13:14 prox-centos7 postfix / smtp [6297]: D84A51807123: to = <zeroxzed@gmail.com>, relay = smtp.yandex.ru [93.158.134.38]: 587, delay = 0.17, delays = 0.02 / 0.01 / 0.13 / 0.01, dsn = 5.7.1, status = bounced (host smtp.yandex.ru [93.158.134.38] said: 553 5.7.1 Sender address rejected: not owned by auth user. (In reply to MAIL FROM command))

This happens because sending system messages comes from the local root user. The sender's name in the letter will be something like this - root@prox-centos7.loc. In this case, prox-centos7.loc is the local server name. I will replace it with the Yandex account.

Replacing sender address in postfix

Let's start by changing the sender so that you can immediately test the sending. To do this, add the following parameter to the postfix /etc/postfix/main.cf config .

smtp_generic_maps = hash: / etc / postfix / generic

I also have the following parameters:

myhostname = prox-centos7 mydomain = prox-centos7.loc mydestination = $ myhostname

You should already have a generic file. If not, create it. Next, add one line to it.

root@prox-centos7.loc root@serveradmin.ru

root@prox-centos7.loc

root@serveradmin.ru

Local server user.

Yandex server user. In this case, the serveradmin.ru domain is tied to Yandex mail.

We restart postfix and check the sending.

The sender is local in the log, but nonetheless, Yandex successfully sent an email, there was no error.

Automatic replacement of the subject of letters in postfix

We turn to replacing the subject of the letter. To do this, add the following parameter to the postfix /etc/postfix/main.cf config .

header_checks = pcre: / etc / postfix / rewrite_subject

Create a rewrite_subject file  with the following contents:

/ ^ Subject: (. *) $ / REPLACE Subject: (prox-centos7) $ 1

This is a regular expression that changes the title of the email starting with Subject. It adds the server name in parentheses to the beginning of the topic - (prox-centos7). You can add something of your own. For those who do not understand the regular season at all, I’ll explain that in this case $ 1 is the initial content of the topic.

We restart postfix and verify by sending a letter through the console.

# df -h | mail -s "Disk usage" zeroxzed@gmail.com

Postfix masquerading or changing outgoing SMTP email or mail address

Address rewriting allows changing outgoing email ID or domain name itself. This is good for hiding internal user names. For example:

SMTP user: tom-01

EMAIL ID: tom@domain.com

Server name: server01.hosting.com

However when tom-01 send an email from shell prompt or using php it looks like it was send from tom-01@server01.hosting.com

In some cases internal hosts have no valid Internet domain name, and instead use a name such as localdomain.local or something else. This can be a problem when you want to send mail over the Internet, because many mail servers reject mail addresses with invalid domain names to avoid spam.

Postfix MTA offers smtp_generic_maps parameter. You can specify lookup tables that replace local mail addresses by valid Internet addresses when mail leaves the machine via SMTP.

Open your main.cf file

# vi /etc/postfix/main.cf

Append following parameter

smtp_generic_maps = hash:/etc/postfix/generic

Save and close the file. Open /etc/postfix/generic file:

# vi /etc/postfix/generic

Make sure tom-01@server01.hosting.com change to tom@domain.com

tom-01@server01.hosting.com tom@domain.com

   user@localdomain.local      account@isp.example.com     @localdomain.local          wholedomain@isp.example.com

Save and close the file. Create or update generic postfix table:

# postmap /etc/postfix/generic

Restart postfix:

# /etc/init.d/postfix restart

When mail is sent to a remote host via SMTP this replaces tom-01@server01.hosting.com by tom@domain.com mail address. You can use this trick to replace address with your ISP address if you are connected via local SMTP.

----------