1920px Postfix architecture.svg
There are many reasons why you would want to configure Postfix to send email using an external SMTP provider such as Mandrill, SendGrid, Amazon SES, or any other SMTP server. One reason is to avoid getting your mail flagged as spam if your current server’s IP has been added to a spam list. Another very common reason would be being stuck behind port 25 (other ports included with some hosts) – host restricted, it’s becoming ever so common now due to the vulnerable nature of a mail-server being on such a common port. To name a few restricting hosts – Linode – Google Cloud.
Before starting this tutorial, you should have:
sudo apt-get update
sudo apt-get install libsasl2-modules
(All commands featured in this guide are intended for non-root users)
In this section, you will install Postfix and set the domain and hostname.
sudo apt-get install postfix
/etc/postfix/main.cf
file with your favorite text editor:sudo nano /etc/postfix/main.cf
1
myhostname = fqdn.example.com
Usernames and passwords are generally stored in a file called sasl_passwd
in the /etc/postfix/
directory. In this section, you’ll add your external mail provider credentials to this file and to Postfix.
If you want to use Mandrill, or SendGrid as your SMTP provider, you may want to reference the appropriate example while working on this section. For Google Apps and Gmail-specific settings (Scroll down)
Open or create the /etc/postfix/sasl_passwd
file, using your favorite text editor:
sudo nano /etc/postfix/sasl_passwd
Add your destination (SMTP Host), username, and password in the following format:
File: /etc/postfix/sasl_passwd
[mail.isp.example] username:password
If you want to use a port other than the default smtp port use the following format:
[mail.isp.example]:587 username:password
Create the hash db file for Postfix by running the postmap
command:
sudo postmap /etc/postfix/sasl_passwd
The /etc/postfix/sasl_passwd
and the /etc/postfix/sasl_passwd.db
files created in the previous steps contain your SMTP credentials in plain text.
For security reasons, you should change their permissions so that only the root user can read or write to the file. Run the following commands to change the ownership to root and update the permissions for the two files:
sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
In this section, you will configure the /etc/postfix/main.cf
file to use the external SMTP server.
/etc/postfix/main.cf
file with your favorite text editor:sudo nano /etc/postfix/main.cf
sasl_passwd
file, then you must use the same port when configuring the relayhost parameter.File: /etc/postfix/main.cf
relayhost = [mail.isp.example]:587
3. At the end of the file, add the following parameters to enable authentication:
File: /etc/postfix/main.cf
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
3. Save your changes
4. Restart the post fix server – “sudo service postfix restart”
Walla. Job done.
This section shows you settings for some popular mail services you can use as external SMTP servers. You may have to do some fine-tuning on your own to avoid Postfix logins being flagged as suspicious.
Use these settings for Mandrill.
/etc/postfix/sasl_passwd
, use the following configuration with your own credentials:File: /etc/postfix/sasl_passwd
[smtp.mandrillapp.com]:587 USERNAME:API_KEY
2. For /etc/postfix/main.cf
, use the following relayhost:
File: /etc/postfix/main.cf
relayhost = [smtp.mandrillapp.com]:587
3. Create the hash db file for Postfix by running the postmap
command:
sudo postmap /etc/postfix/sasl_passwd
4. Restart postfix
sudo service postfix restart
Use these settings for SendGrid.
/etc/postfix/sasl_passwd
, use the following configuration with your own credentials:File: /etc/postfix/sasl_passwd
[smtp.sendgrid.net]:587 USERNAME:PASSWORD
2. For /etc/postfix/main.cf
, use the following relayhost:
File: /etc/postfix/main.cf
relayhost = [smtp.sendgrid.net]:587
3. Create the hash db file for Postfix by running the postmap
command:
sudo postmap /etc/postfix/sasl_passwd
4. Restart Postfix:
sudo service postfix restart
5. Go enjoy.
This post was last modified on 13 November 2020 3:30 AM
British high street chain WH Smith has recently revealed that it was hit by a…
As banks worldwide roll out Voice ID as a means of user authentication over the…
In the era of digital transformation, cybersecurity has become a major concern for businesses. When…
In today's digital age, cybersecurity threats have become a significant concern for businesses of all…
The RIG Exploit Kit is currently in the midst of its most productive phase, attempting…
One of the most transformational technologies of our time, artificial intelligence (AI), has quickly come…
Leave a Comment