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

Configure postfix to relay through Gmail

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.

YouTube video

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, 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 /etc/postfix/sasl/sasl_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/ file.

mydestination = $myhostname, localhost, localhost.localdomain
relayhost = []:587
mynetworks = [::ffff:]/104 [::1]/128

Also, add the following to the end of /etc/postfix/ 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.

Subject: Test mail #1
This is just a test email

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

* * * * * echo "Hello world"

YouTube video

Please let me know if you have any questions about configuring Postfix to use Gmail SMTP on Ubuntu in the comments below.


Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

10 Responses

  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!

  2. Hello Tony,

    nice tutorial thanks for that.

    can you modify “SMPT Postfix tutorial” to “SMPT Postfix tutorial” ?

  3. Thanks for the great tutorial. One observation on my systems was that sendmail is in /usr/sbin which is not in the default search path for a normal user. This held up getting it working for a couple minutes

  4. Hi Tony,
    Thanks for the nice manual.
    I ‘d like to correct NAMASIVAYAM 🙂
    can you modify “SMPT Postfix tutorial” to “SMTP Postfix tutorial” ?

    It’s the Simple Mail Transfer Protocol

    I guess your manual will be more easy to find.
    Thank you,

  5. Hi Tony,
    Thank you so much for this simplified manual.
    Can you please help me with the attachments in the email? I tried with mutt but I received the exact command printed in the email used to attach a file.

Leave a Reply

Your email address will not be published. Required fields are marked *