How to Send Email on Ubuntu from Gmail (SMPT Postfix tutorial)

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

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.

Postfix configuration screen with internet site selected

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:

[smtp.gmail.com]:587 microdomainz@gmail.com:qodjkozoaqdmyqll

In this case, smtp.gmail.com is the address of the Gmail SMPT 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 /etc/postfix/sasl/sadl_passwd.db.

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 /etc/postfix/main.cf file.

mydestinatoin = $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 tony@tonytravels.com
To: tony@tonytravels.com
Subject: Test mail #1
This is just a test email

Postfix will also use Gmail to send crontab emails with mailto.

MAILTO="tony@tonytravels.com"
* * * * * 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.


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

1 thought on “How to Send Email on Ubuntu from Gmail (SMPT Postfix tutorial)”

  1. I just want to thank you for this tutorial. I explored around ten different pages with insufficient information and spent hours trying to make postfix work. Yours is the only tutorial that actually worked!

    Reply

Leave a Comment