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:
[smtp.gmail.com]:587 microdomainz@gmail.com:qodjkozoaqdmyqll
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 /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/main.cf
file.
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 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.
10 Responses
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!
You’re welcome!
Hello Tony,
nice tutorial thanks for that.
can you modify “SMPT Postfix tutorial” to “SMPT Postfix tutorial” ?
Hey, mydestinatoin should be mydestination
Thanks for pointing this out 🙂
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
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,
Erik
Thank you for pointing that out!
Tony – I followed your video and text to the letter, and nothing happen. No email! Where did I fail?
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.