How to: Use Postfix To Send Outbound Email as a Gmail Relay
You can use Postfix to send emails directly from the command line. For this example, we'll be making use of Gmail’s SMTP server.
Postfix is available to install from the built-in APT package manager. First, we will update the packages and then install it. We're also going to be grabbing the libsasl2-modules package as it will be needed later on.
apt-get update && apt install postfix libsasl2-modules
During the installation, you may be prompted to configure Postfix. If it doesn't you can use the following command:
sudo dpkg-reconfigure postfix
For General type of mail configuration, we're going to pick Internet Site
When it asks about your Fully Qualified Domain Name (FQDN), you can choose to use your domain, or you can also choose your system's hostname.
From here on, we're going to continue with a Gmail account that does not have 2-Step Verification (2FA) enabled.
If you need to use an account that has 2FA enabled, you'll need to go to https://security.google.com/settings/security/apppasswords and generate an Application Password for Postfix.
Once Postfix is installed, we need to store our dummy Gmail account's information somewhere.
(Highly advised to create an example Gmail to test with before storing your own credentials as the files that contain the information store them in PLAIN TEXT meaning anyone with access will be able to read those credentials)
This can be done with the following:
sudo nano /etc/postfix/sasl/sasl_passwd
Inside this file, insert the following: (username and password need to be changed to the information that matches the account you wish to use)
After that is finished, go ahead and generate the hash database for Postfix:
sudo postmap /etc/postfix/sasl/sasl_passwd
These files that we just created store your credentials in PLAIN TEXT. In order to secure this information, we're going to restrict access to the files so that only the root user is allowed in.
sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
Next up, we're going to configure Postfix as a relay for Gmail's SMTP server.
sudo nano /etc/postfix/main.cf
Inside this file, make the following changes:
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
Now it's time to restart Postfix:
sudo systemctl restart postfix
The last step is to test and see if we can send an e-mail. When performing the next set of commands, do not copy and paste them all at once. Insert or type them out one at a time.
sendmail email@example.com From: firstname.lastname@example.org Subject: Example Email This is an example email .
NOTE - we've intentionally avoided using the word 'test' in the e-mail subject. In my testing, I've run into issues where e-mails were either delayed or outright rejected when I included the word test in my subject lines. To simplify the process for myself, I've just begun to avoid using that word when testing e-mail systems. Your mileage may vary in this regard.
If you aren't receiving your example e-mails, you can try the following command to see what changes are happening in real-time.
sudo tail -f /var/log/syslog