In this tutorial, you will learn how to send email from Gmail with Postfix on Ubuntu. In other words, we will configure Ubuntu to use the Gmail SMTP mail servers to send email. Prerequisites are that you have sudo access to a modern version of Ubuntu and a Google account.
1. Install Postfix
First thing, update Ubuntu so it has the latest packages installed.
sudo apt-get update && sudo apt-get upgrade
Next, install Postfix which is what we will be using to send email from your Gmail account.
sudo apt-get install postfix
The first part of the Postfix installation is a text-base user interface. Use you keyboard to select Internet Site for the type of mail configuration.
On the next screen, enter the domain name of your server. Please note that this doesn’t have any impact on your ability to send mail from Gmail, so if you don’t have a domain name, that’s okay.
2. Get a Google App Password
Rather than using the password that you use to sign into your Gmail inbox, it’s better to generate an app password that can be used instead. If you’re not familiar with that process, please watch this tutorial which will show you how to get an app password for your Google Account.
3. Configure SASL with Your Gmail Credentials
SASL, which stands for Simple Authentication and Security Layer, is basically what it sounds like. SASL makes it easy for applications to authenticate with various internet technologies.
Create a the file
/etc/postfix/sasl/sasl_passwd and add your Gmail address and app password to it like this:
In this case, smtp.gmail.com is the address of the Gmail SMTP server and 587 is the SMTP port.
Next, create a hash database file with the following postmap command.
sudo postmap /etc/postfix/sasl/sasl_passwd
Note that now you will have a database file at
In order to protect the plain-text password, change the owner and permission for the SASL files as follows:
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
One more configuration file to edit here. In order to tell Postfix to use Gmail servers to send mail, set the relay value in the
mydestination = $myhostname, localhost, localhost.localdomain relayhost = [smtp.gmail.com]:587 mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
Also, add the following to the end of
/etc/postfix/main.cf to enable SASL authentication for Postfix.
# Enable SASL authentication 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
Finally, restart Postfix to apply our configuration changes.
sudo systemctl restart postfix
4. Test Sending Email from Gmail
Use the sendmail command to send a test email.
sendmail firstname.lastname@example.org To: email@example.com Subject: Test mail #1 This is just a test email
Postfix will also use Gmail to send crontab emails with mailto.
MAILTO="firstname.lastname@example.org" * * * * * echo "Hello world"
Please let me know if you have any questions about configuring Postfix to use Gmail SMTP on Ubuntu in the comments below.