Postfix is an amazing free and open-source tool that acts as Mail Transfer Agent (MTA). It is a great tool to route and send emails to anyone. It comes quite handy in the work of DevOps. The best way to utilize this tool is to use it for email alerts on a Linux server.
In this post, we will learn how we can setup postfix on your Linux server and send mail to other emails in the easiest way.
Step 1. Install all the required packages by installing mailutils.
$ sudo apt-get update $ sudo apt-get install mailutils
Step 2. Select “Internet Site” in postfix configuration prompt
And in 2nd prompt, you can give any name like your server name or domain name
Step 3. Edit the main.cf of postfix for gmail smtp configuration
$ sudo nano /etc/postfix/main.cf
Look for relayhost and enter [smtp.gmail.com]:587
And at the bottom of the file add these lines below.
smtp_sasl_auth_enable = yes smtp_sasl_security_options = noanonymous smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd smtp_tls_security_level = encrypt smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt smtp_sasl_mechanism_filter = login
After that save the file and run the reload command.
$ sudo postfix reload
Note: Reload postfix whenever you make any changes in main.cf file.
Step 4. Create sasl_passwd file and add our smtp details
We will create a sasl_passwd file with the same path we have mentioned in the previous step.
$ sudo nano /etc/postfix/sasl/sasl_passwd
And paste this
Replace the <email-id> and <email-password> with your credentials
Then map credentials with this command
$ sudo postmap /etc/postfix/sasl/sasl_passwd
Step 5. Make sure you have allowed less secure apps in your given gmail account in the previous step. Follow this link here
Step 6. Test the setup by sending a mail
$ echo “test alert demo email.” | mail -s “demo subject” <any-email>
You will receive an email on your given email id in this command.
To debug this, we can check the logs by
$ sudo tail -f /var/log/mail.log