Configure Postfix to Use Gmail SMTP on Ubuntu

If you want to use a Gmail account as a free SMTP server on your Ubuntu-Linux server, you will find this article useful. This guide is tested with Ubuntu 12.04. If you face any issue, feel free to use comments-section below.

Check if mails are sent via Gmail SMTP server

To further verify, if mail sent from above command is actually sent via Gmail’s SMTP server, you can log into Gmail account USERNAME@gmail.com with PASSWORD and check “Sent Mail” folder in that Gmail account. By default, Gmail always keeps a copy of mail being sent through its web-interface as well as SMTP server. This logging is one strong reason that we often use Gmail when mail delivery is critical.

Once configured, all emails from your server will be sent via Gmail. This method will be useful if you have many sites on your server and want them all to send emails via Gmail’s SMTP server.

Alternatively, you can use a plugin like WP Mail SMTP so that mails from your particular WordPress site will be sent using Gmail’s SMTP server.

Please note that Gmail’s SMTP server has a limit of 500 emails per day. So use wisely! 🙂

XXXx@XXXXXX:/etc/postfix$ echo “Test mail from postfix” | mail -s “Test Postfix” XXXXX@XXX.com
The program ‘mail’ is currently not installed. To run ‘mail’ please ask your administrator to install the package ‘mailutils’

Worked great! However, in my case I had to do one more thing which I thought I’d share here. After sending the test email, I got a “Suspicious sign-in prevented” email from google in my inbox. So, I went to google.com to search for a solution, saw a big red banner saying something like “someone tried to login to your email from a new application, was it you?” I clicked through several screens to say “yes, this was me”.

Oh my goodness!… I tried everything and couldn’t get SASL Authentication working. I knew I was close. Google even warned me of the “Suspicious Activity” which I confirmed, but then I saw the link you posted:

Hi,
I configured postfix as above, but unfortunately send mail root through root@ not from gmail , and no email at send mail of gmail a/c, I double checked all config exactly as per this doc, Can you please help me , Is their any additional configuration I need set up?

Thanks for the promp response and sorry for the late comeback, but I found out that at the place where i was trying to set this up smtp is a no no, it has been disabled on the network for security reasons.

Not all outbound mail. 99% of the users use windows and have outlook configured through the company servers. I’m the oddball there because i’m trying to get a backup application running on a linux pc for some of the older computers that can’t be replaced for some reason or another, and when i say old i mean going on 20 years, they’re practicaly the legal drinking age. I was trying to configure email client on that. Thanks for all the help.

Hi Rahul, Its a great post, helpful for me, The issue I posted earlier is resolved, By default sendmail also installed and sending mail through sendmail, I removed sendmail, now everything is as expected. Thank you somuch

Just wanted to extend my thanks to you for this succinct write-up. I have setup mail on Ubuntu servers before, but this was my first time attempting SMTP via Postfix, and this was a very simple and quick mail setup that just works.

Hey man,
You’re really the best… I’ve been on this for over a week but U did it in less than 30 minutes!!! Thanks a million. I hope I will be able to send with attachments too!!!
Thany you so much… You deserve the best

I have ssmtp on my server, but cron did not like to use it. I assume it’s not the cause of this problem.
also: I use 2-factor auth, but I gave it an application-specific password. – and it does not seem to be an authentication problem.
Please help me. 🙂

Also, I did not get a wizard, you mention that I should configure as “internet site”.
I changed
myhostname ODIN2.example.com

now the error is still the same, except for the destination address being all wrong
Nov 8 12:54:12 ODIN2 postfix/error[25061]: 871077700701: to=, relay=none, delay=0, delays=0/0/0/0, dsn=5.0.0, status=bounced (odin2.example.com)

ow can I do this” select Internet Site and for FQDN use something like mail.example.com”

This tutorial has been very helpful many times. However, I am stuck with a couple of clients using Zoho (at my recommendation as it is free-er)

Do you have any experience setting up postfix with Zoho? If you do, would you be willing to write another lovely tutorial?
The internet has failed me and I was not able to find anything but the following:https://forums.zoho.com/topic/setup-postfix

Replacing with [smtp.zoho.com]:465 in main.cf unfortunately doesn’t help, since Postfix doesn’t support legacy TLS. I happened to get Postfix work with Zoho email while setting up gitlab. Details are here. Details Link.

Hi, thanks for your guide. I got it to work for outgoing emails from root@localhost. What must i do to get it to work for incoming emails sent to gmail? I tried searching on the internet but came up with nothing :(. Hopefully you have a solution for me.

Fantastic tutorial but I’m falling at the first hurdle;-
Reading packages lists…Done
Building dependency tree
Reading state information… Done
Package mailutils is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or is only available from another source
E: Package ‘mailutils’ has no installation candidate
E: Unable to locate package libsas12-2
E: Unable to locate package libsas12-modules

Reverse Dns not working in my fqdn.its giving 3(NXDomain)error.i enabled smtp my gmail account whose details i saved in/etc/postfix/sasl_passwd so whenever i send to any other gmail account via command prom then its working but if i send it to username@myfqdn then its not sent via gmail accnt .i m not able to send any mail to my fqdn.for example if fqdn is myhost.mail.com then i can send mail to username@myhost.mail.com but not to myhost.mail.com.when i try to send mail to myhost.mail.com then it gives failure report in my gmail account…if my hostname is myhost and fqdn is myhost.mail.com then sending mail to abc@mail.com is giving failure report but abc@myhost.mail.com is sending mail bt not via gmail.

Hello sir, thank you for your great tutorials, i know this is not the right place for this but unfortunely im a windows user with xampp server, i configured my php.ini and my sendmail.ini for gmail smtp, my php mail sender script says mail successfully sent, but i dont recive any thing, i tried all possible sollutions on internet but still got this problem! so maybe you cn helep me 🙁 PLEAZEEE!

Thank you for this tutorial! I got my gmail smtp on postfix to work, Quick question which I really pray u have an answer for it! How do I configure fail2ban for postfix if I followed your tutorial? thank you!!!

The same way you would have configured when not using Gmail as reply in postfix. I guess fail2ban comes into picture if you are running a public SMTP server. In this case, it is assumed that postfix is not accessible to the outside world via SMTP.

Gmail accounts has very low sending limits. So we use this kind of setup only in small apps for notification email types only.

Hi, I follow your instruction to send mail via gmail using postfix. However, my network is working behind proxy with userid and password. Will you help me setting up these parameters so that I also feel happy to send mail using this way.

Postfix via Gmail shouldn’t care about domain or IP. This is used mostly in locations where server/machine IP is dynamic.

I am assuming that you have a postfix+gmail setup on your laptop which is you are using at home/work.

When at work try ping yourdomain.name. Repeat same commands at home. If there is a difference in IP, try to offset that difference by modifying local /etc/hosts.

Another case, is postfix+gmail setup is present at work machine but postfix has smtpd_recipient_restrictions enabled with setting reject_non_fqdn_sender. Remove reject_non_fqdn_sender so your (senders) IP/FQDN will not be verified.

hi. really nice tutorial. i got it working simply by following this tutorial.
when i send emails, the recipient sees root (as name, the email address appears correctly).
it appears as root is there a way to change the root to another name.
when i send from my thunderbird using this email address it appears the name i have setup.

I get the following log message from gmail: “….The IP you’re using to send mail is not authorized to 550-5.7.1 send email directly to our servers. Please use the SMTP relay at your 550-5.7.1 service provider instead…. ”
Could you suggest a workaround?

I have installed it as you explained it here step by step for my WordPress site. I use Contact Form7 plugin for creating my forms. So I am able to send emails, but the Sender address in all those emails is what I have entered in sasl_passwd file. And it ignores the Sendaddress in the ContactFrom7 plugin. Is there a solution for this ?

Rahul: It didn’t work perfectly initially. But commenter’s followup questions echoed my issues. The machine said “bind 0.0.0.0 port 25: Address already in use. ” You suggested that this might be due to “some other mail program is already present in your system.” I did “apt-get –purge remive sendmail” to get rid of my best guess, but apparently sendmail it had its claws sunk deep into the OS: I continued to get errors when I did your example test.

But one reboot and Hey Presto! it sends mail!

Many thanks, Rahul! It works good. Thank you for writing up this very handy how-to.