Your business needs a fast and reliable Free SMTP server and relay provider. Use your preferred Message Delivery Agent or Message Transfer Agent (MTA).
Use Case ScenarioLet’s say there are two servers: server A and server B. You have on server A with Postfix as the SMTP server. You can use it to send emails directly to recipients, because port 25 isn’t blocked.Later you use server B to set up a website, which needs to send notification emails to users. You can set up another mail server on server B, but it’s a waste of time and hardware resources. A more sensible solution is to install Postfix SMTP server on server B and configure it to send emails via server A, which can relay emails from server B to the final recipients. Server A has built up its IP reputation, so you don’t have to build IP reputation for server B.If you run WordPress on your own Linux server, I recommend you follow this tutorial to set up Postfix SMTP relay.
This way, you can get rid of SMTP plugins in WordPress. WordPress plugins slow down your site and they can be vulnerable, such as, which allows unauthorized users to modify WordPress options and execute malicious code. My site has been compromised once, because of vulnerability in WordPress plugin. So I get rid of as many plugins as I can, when I can implement the same function with the underlying operating system.Without further ado, let’s get started.
Installing Postfix SMTP Server on Server BFirst, let’s install Postfix SMTP server on server B with the following command. If Postfix is already running on server B, then skip installing Postfix, but you still need to install the libsasl2-modules package.
Sudo apt install postfix libsasl2-modulesWhen you see the following message, press Enter to choose the second option: Internet Site.Next, set the system mail name. For example, I enter my domain name www.linuxbabe.com. Note that you should not enter your main domain name like linuxbabe.com, because that will make server B as a destination for your main domain name, which means emails generated from server B for will be sent to server B itself, instead of server A.Once Postfix SMTP server is installed on server B, let’s configure SMTP relay. Postfix SMTP Relay via port 587Edit the Postfix main configuration file on server B. Sudo nano /etc/postfix/main.cfFind the following line. Relayhost =By default, its value is not set. You need to set the hostname of server A (your mail server) as the relay host like below.
Relayhost = mail.linuxbabe.com:587Then add the following lines to the end of this file to configure SASL authentication. We specify that the /etc/postfix/saslpassword file contains the username and password. # outbound relay configurationssmtpsaslauthenable = yessmtpsaslpasswordmaps = hash:/etc/postfix/saslpasswdsmtpsaslsecurityoptions = noanonymoussmtptlssecuritylevel = mayheadersizelimit = 4096000Save and close the file.
Next, you should create a dedicated email account on your mail server, so server B can use this email account to login via port 587. After that, create the /etc/postfix/saslpasswd file.
Sudo nano /etc/postfix/saslpasswdAdd the SMTP relay host and SMTP credentials to this file like below. Replace these values with the hostname of your own mail server, the email account and password. Notice that there’s a colon between the email account and password. Mail.linuxbabe.com:587:passwordSave and close the file.
Then create the corresponding hash db file with postmap. Sudo postmap /etc/postfix/saslpasswdNow you should have a file /etc/postfix/saslpasswd.db. Restart Postfix for the changes to take effect.
Sudo systemctl restart postfixBy default, saslpasswd and saslpasswd.db file can be read by any user on the server. Change the permission to 600 so only root can read and write to these two files. Sudo chmod 0600 /etc/postfix/saslpasswd /etc/postfix/saslpasswd.dbFrom now on, Websites on server B can use Postfix to send emails, which will be relayed through your mail server. Note that many web applications provides two email-sending modes:. SMTP.
SendmailSMTP usually refers to the SMTP relay function in the web application itself and sendmail refers to using the SMTP server on the underlying operating system. You need to choose the sendmail option in order to use Postfix SMTP relay.
If you installed SMTP plugin on your WordPress site, remove the SMTP plugin and WordPress will use Postfix SMTP relay. If You Have iRedMail on Server AIf you used iRedMail to set up mail server on server A, then the iRedAPD policy daemon will likely to reject email relay from server B, because the sender is not same as SMTP authenticate username. To solve this problem, we need to add the SMTP authentication username to the allowed list.Edit the iRedAPD configuration file.
Sudo nano /opt/iredapd/settings.pyAdd the following line at the end of the file. Replace the red text as necessary. ALLOWEDLOGINMISMATCHSENDERS = ' 'Save and close the file. Then restart iRedAPD for the change to take effect. Sudo systemctl restart iredpad Preventing Spammers on Server BBy default, Postfix SMTP server listens on all active interfaces on the machine.
Since the Postfix SMTP server on server B is only used for sending transactional emails to users, we can make it listens on localhost only, so bad actors can’t send spam to it.Edit the Postfix main configuration file on server B. Sudo nano /etc/postfix/main.cfFind the following line. Inetinterfaces = allChange it to: inetinterfaces = loopback-onlySave and close the file. Restart Postfix for the change to take effect. Sudo systemctl restart postfix Setting the From Address, From Name and Return-PathBy default, the From address and From name are the same as the email account that is used to authenticate login, and the return-path will be something like. You can set custom From address, From name and Return-Path in your web application.Let’s use WordPress as an example. You can add the following lines in your WordPress theme’s functions.php file to override the default From address, From name and return-path.
Replace the red text as necessary. You should create the From email address on your mail server to prevent send failure.
Domain2.com:passwordSave and close the file. Then create the hash db file.
Sudo postmap /etc/postfix/relaybysendersudo postmap /etc/postfix/saslpasswdRestart Postfix SMTP server for the changes to take effect. Sudo systemctl restart postfixFrom here on out, emails with domain1.com in the Envelope From address will be relayed via mail.domain1.com and emails with domain2.com in the Envelope From address will be relayed via mail.domain2.com. Emails with other domains names in the Envelope From address will be relayed via the host specified for relayhost parameter.Mail.domain1.com and mail.domain2.com can point to the same IP address, which means the two domain names are using the same mail server. You can check one of the following tutorials to host multiple domains on a single mail server.You can also host emails on different servers for the two domain names. If you have multiple WordPress sites on server B, you should also change each functions.php file in your WordPress themes to set custom From address and names for each domain name.
Removing Sensitive Information from Email HeadersBy default, Postfix SMTP server will add a Received: email header, recording the IP address of server B, which can leak the IP address of your website (If it’s behind CDN). You can tell Postfix to ignore it. Create a header check file on server A. Sudo nano /etc/postfix/headerchecksPut the following lines into the file. /^Received:/ IGNORESave and close the file. Then edit the Postfix main configuration file. Sudo nano /etc/postfix/main.cfAdd the following two lines at the end of the file.
Headerchecks = regexp:/etc/postfix/headerchecksSave and close the file. Then run the following command to rebuild hash table. Sudo postmap /etc/postfix/headerchecksReload Postfix for the change to take effect. Sudo systemctl reload postfixNow Postfix won’t include those sensitive information in email headers.
Note that some folks may also like removing the MIME-Version header. I don’t recommend it, because this will cause DKIM verification failure. ConclusionI hope this tutorial helped you set up SMTP relay between 2 Postfix SMTP servers.
As always, if you found this post useful, then to get more tips and tricks. Take care 🙂.