There are numerous cases in which some businesses want to use a free Gmail account for outgoing emails. That can vary from informational emails created by an application, or email alerts of a monitoring system that goes to your internal team and wants to get a paid license of your existing domain. I will explain in this step-by-step guide how to use Postfix to relay outgoing emails through a Gmail account.
Prerequisites:
- A Gmail account with 2-step verification.
- An App Password generated for the Gmail account.
- A Linux system (in our case we will use Ubuntu Server).
- The application you want to relay from should be on the same server. I will create a future article on how to configure postfix to accept connections from other servers too.
- Some patience even the procedure is very straightforward 😊.
Prepare Gmail Account:
Go to the Gmail account that you want to use for this purpose and click on Account -> Manage your Google Account.
Go to Security and Enable 2-Step Verification with authentication method you prefer.
Then go to search and type “App passwords”.
Choose a name and create an App password for the postfix authentication. Keep the password safe as we will need it later.
Postfix Installation:
1 | sudo apt install postfix -y |
During installation the wizard might appear. Select “Internet Site.” Also set the system mail name (this doesn’t affect the relaying so you can pick your preferred domain here).
Ensure Postfix is running:
1 2 | sudo systemctl start postfix sudo systemctl enable postfix |
Configure Postfix:
Open main configuration file.
1 | sudo nano /etc/postfix/main.cf |
Modify or add the following lines to configure Gmail as the relay host. Some of the following lines already exist in the configuration, so if you intend to copy-paste the code, below make sure you find and comment out or delete those before you save the file.
1 2 3 4 5 6 | relayhost = [smtp.gmail.com]:587 smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_security_level = encrypt smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt |
Setup Authentication:
Create the /etc/postfix/sasl_passwd file to store Gmail credentials.
1 | sudo nano /etc/postfix/sasl_passwd |
Add the following line, replacing your-email@gmail.com and your-app-password with your Gmail credentials.
1 | [smtp.gmail.com]:587 your-email@gmail.com:your-app-password |
Secure the file.
1 | sudo chmod 600 /etc/postfix/sasl_passwd |
Hash the file to create a database for Postfix.
1 | sudo postmap /etc/postfix/sasl_passwd |
Restart Postfix.
1 2 | sudo systemctl reload postfix sudo systemctl restart postfix |
Test the configuration:
Install the Mailutils package to send a test email directly from shell.
1 | sudo apt install mailutils -y |
Send a test email.
1 | echo "This is a test email from Postfix on Ubuntu" | mail -s "Test Email" recipient@example.com |
Check the Postfix logs for any issues.
1 | sudo tail -f /var/log/mail.log |
Conclusion:
By following this step-by-step guide in how to use Postfix to relay outgoing emails through a Gmail account, you will have all the benefits of using a fully compliant mailbox and you won’t have to use the internal Sendmail account which needs a serious level of configuration in order to support all the modern authentication mechanisms that email servers using (DKIM, DMARC etc.). Here we have a list with the best open-source IT monitoring tools for 2024. If you are looking such tool here you can learn more .
If the above sounds confusing, very technical, or just time consuming for you current capacity, we are here to help Contact us by Clicking Here. We are helping businesses to stabilizing and optimizing their environments. We also offer monitoring as a service if you just want us to keep an eye and alert you if something has indications of the will stop working soon. Here is an article related to monitoring