I am inexperienced in configuring mail systems. I am using Ubuntu #54~20.04.1-Ubuntu.

How can I configure my computer to send emails using a gmail account, with postfix?
I mean not to have any other paid mail server or domain involved (EDIT: I could configure my PC to work as a server, but I mean to avoid requiring paid registration in DNSs). Is this is even possible?

I tried a few things, which I will describe below, citing the sources I used.
I have read other sources, but I could not figure out what to do. As I understand, most of these other sources assume I have some other server or address I can use (typically exemplified as example.com), which I don't have, and I guess I can dispense with.

  1. I followed this basic guide, which seemed to be useful for many others. After issuing

    echo "Test mail thingy" | mail -s "Test Postfix Subject" mygmaildest@gmail.com

    I did not get an email in mygmaildest@gmail.com, and this is the relevant tail of /var/log/mail.log (line numbers added for reference)

     1 Apr 18 08:55:03 MyComputer postfix/postfix-script[1092678]: refreshing the Postfix mail system
     2 Apr 18 08:55:03 MyComputer postfix/master[3124]: reload -- version 3.4.13, configuration /etc/postfix
     3 Apr 18 08:55:24 MyComputer postfix/pickup[1092682]: 06004443921: uid=1000 from=<MyUser@MyComputer>
     4 Apr 18 08:55:24 MyComputer postfix/cleanup[1092704]: 06004443921: message-id=<20210418115524.06004443921@MyComputer.MyISP_URL>
     5 Apr 18 08:55:24 MyComputer postfix/qmgr[1092683]: 06004443921: from=<MyUser@MyComputer>, size=391, nrcpt=1 (queue active)
     6 Apr 18 08:55:24 MyComputer postfix/error[1092706]: 06004443921: to=<mygmaildest@gmail.com>, relay=none, delay=0.03, delays=0.02/0/0/0.01, dsn=5.0.0, status=bounced ([smtp.gmail.com]:587)
     7 Apr 18 08:55:24 MyComputer postfix/cleanup[1092704]: 09DD4443938: message-id=<20210418115524.09DD4443938@MyComputer.MyISP_URL>
     8 Apr 18 08:55:24 MyComputer postfix/bounce[1092707]: 06004443921: sender non-delivery notification: 09DD4443938
     9 Apr 18 08:55:24 MyComputer postfix/qmgr[1092683]: 09DD4443938: from=<>, size=2359, nrcpt=1 (queue active)
    10 Apr 18 08:55:24 MyComputer postfix/qmgr[1092683]: 06004443921: removed
    11 Apr 18 08:55:24 MyComputer postfix/local[1092708]: 09DD4443938: to=<MyUser@MyComputer>, relay=local, delay=0.01, delays=0/0/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
    12 Apr 18 08:55:24 MyComputer postfix/qmgr[1092683]: 09DD4443938: removed

    I don't know how to interpret this log. I note that line 11 states status=sent.

  2. I added -a "From: username@gmail.com" (ref). I did not get the mail either. Lines in /var/log/mail.log that changed as compared to above are marked with *. Now I have status=bounced in line 11.

     3*Apr 18 09:54:43 MyComputer postfix/pickup[1098199]: 0353244348F: uid=1000 from=<mygmailsrc@gmail.com>   <-- CHANGED
     4 Apr 18 09:54:43 MyComputer postfix/cleanup[1098387]: 0353244348F: message-id=<20210418125443.0353244348F@MyComputer.MyISP_URL>
     5*Apr 18 09:54:43 MyComputer postfix/qmgr[1098200]: 0353244348F: from=<mygmailsrc@gmail.com>, size=384, nrcpt=1 (queue active)   <-- CHANGED
     6 Apr 18 09:54:43 MyComputer postfix/error[1098389]: 0353244348F: to=<mygmaildest@gmail.com>, relay=none, delay=0.03, delays=0.02/0/0/0, dsn=5.0.0, status=bounced ([smtp.gmail.com]:587)
     7 Apr 18 09:54:43 MyComputer postfix/cleanup[1098387]: 06E2F4436C3: message-id=<20210418125443.06E2F4436C3@MyComputer.MyISP_URL>
     8 Apr 18 09:54:43 MyComputer postfix/bounce[1098390]: 0353244348F: sender non-delivery notification: 06E2F4436C3
     9 Apr 18 09:54:43 MyComputer postfix/qmgr[1098200]: 06E2F4436C3: from=<>, size=2364, nrcpt=1 (queue active)
    10 Apr 18 09:54:43 MyComputer postfix/qmgr[1098200]: 0353244348F: removed
    11*Apr 18 09:54:43 MyComputer postfix/error[1098389]: 06E2F4436C3: to=<mygmailsrc@gmail.com>, relay=none, delay=0.01, delays=0/0/0/0, dsn=5.0.0, status=bounced ([smtp.gmail.com]:587)   <-- CHANGED
    12 Apr 18 09:54:43 MyComputer postfix/qmgr[1098200]: 06E2F4436C3: removed


  1. Prior to my attempts today, I already had postfix installed. I do not recall when and why I installed it. I do not recall either going through the postfix configuration screen. I know I can do it with sudo dpkg-reconfigure postfix, but I don't know which is the proper option.

  2. This adds smtp_tls_CAfile = /etc/postfix/cacert.pem to /etc/postfix/main.cf, instead of smtp_tls_CApath ..., and later cats a specific .pem to cacert.pem. I don't have cacert.pem. I guess both options can work if properly configured.

  3. I have lots of soft links in /etc/ssl/certs/. I am not sure I need to add anything.

  4. I used my actual source gmail and corresponding password in /etc/postfix/sasl_passwd.


  1. How can I configure postfix to send emails via gmail (2FA problems)?
  2. SETUP for sending Mail via postfix to GMAIL to use ssmtp instead of postfix.
  3. Setting gmail as default for sending files via email using gnome-gmail or desktop-webmail.
  4. https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/
  5. Sending email from Gmail account to local Ubuntu server
  6. Ubuntu 20.04 Mail Setup Setting the Default From Address
  7. https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-18-04
  8. https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-on-ubuntu-20-04
  9. https://www.linuxbabe.com/mail-server/setup-basic-postfix-mail-sever-ubuntu
  10. https://www.cyberciti.biz/faq/how-to-configure-postfix-relayhost-smarthost-to-send-email-using-an-external-smptd/
  11. https://ubuntu.com/server/docs/mail-postfix
  12. https://devops.ionos.com/tutorials/configure-a-postfix-relay-through-gmail-on-ubuntu/
  • I recommend msmtp. It is easy to set up. It only allows sending mail, no receiving. postfix is a more complex send and receive solution you don't need.
    – user68186
    Apr 18, 2021 at 13:29
  • I recommend nullmailer it's orders of magnitude easier to set up than postfix. Apr 18, 2021 at 13:36
  • @user68186 - I read your comment. Perhaps I try msmtp. But I meant to work with postfix, as I guess in the (near) future I will want to receive email also. Apr 18, 2021 at 13:37
  • @OrganicMarble - I read your comment. Perhaps I try nullmailer. Can it also receive email? I guess in the (near) future I will want to receive email also. Apr 18, 2021 at 13:38
  • Indepth postfix tutorial here: arstechnica.com/information-technology/2014/02/… Apr 18, 2021 at 13:39

2 Answers 2


A step-by-step guide to send email with postfix via Gmail.

I finally managed to send email via Gmail. The two answers by Raffa (1, 2) and further comments helped me a lot. But there was still further research and tinkering needed.

Here, the (hopefully) complete guide.

  1. Install postfix and other packages.

    $ sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
  2. The configuration post-install will show up. If not, use $ sudo dpkg-reconfigure postfix. For the successive screens, use: 1. Satellite system, 2. MyServer (replace with the name of your server), 3. [smtp.gmail.com]:587, 4. root, 5 and following: leave the defaults.

  3. Enable you gmail account to interact with postfix. There are two options for this.

    4.1. (Preferred, more secure method) Activate 2-step verification in your Google account if not activated yet. Then generate an App password for Postfix. You will need this password later.

    4.2. If not using 2-step verification, enable less secure apps.

  4. Add the following lines to /etc/postfix/main.cf

    relayhost = [smtp.gmail.com]:587
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options = 
    smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
    smtp_use_tls = yes
  5. Create/edit /etc/postfix/sasl_passwd so it contains

    [smtp.gmail.com]:587    mygmailsrc@gmail.com:<password>

    <password> should be replaced by the account password if not using 2-step verification, or the generated App password otherwise.

  6. Use the following commands

    $ sudo chmod 600 /etc/postfix/sasl_passwd
    $ sudo postmap /etc/postfix/sasl_passwd
    $ sudo systemctl restart postfix.service
  7. Send email with

    $ echo "This is the body of an encrypted email" | mail -s "This is the subject line" mygmaildest@gmail.com
  • You forgot relay_domains = gmail.com. As I recall, without it, you won't be able to email other gmail addresses.
    – heynnema
    Apr 19, 2021 at 13:52
  • @heynnema - Do you mean adding that line to /etc/postfix/main.cf? I don't have such line, and I could send from one gmail account to another. I verified receiving the email. Apr 19, 2021 at 14:10
  • As I recall, without this line, there were SOME addresses that didn't work... I thought it was other gmail addresses... but I'm old... and my memory isn't what it used to be :-)
    – heynnema
    Apr 19, 2021 at 14:13
  • @heynnema - If I find any issue, I would come back and fix the answer. So far, it is working like a charm! It took a while to find out all the tiny details... but it paid. Apr 19, 2021 at 14:15
  • My postfix setup is similar to yours, but with some additional items that I had to figure out... like aliases, etc. It was a pain to figure it all out, using all of the various doc sites. See man 5 aliases.
    – heynnema
    Apr 19, 2021 at 14:18

It appears that you are not using a relay email / host and you are trying to send email directly from your machine which as you explained has no qualified domain name... let alone the other requirements like reverse DNS record and MX record.

It is clear in your logs that no valid relay is used:




And you are wondering why your emails bounce back!

They will bounce back forever no matter what you do.

  • A domain name is a name with a common TLD like example.com, example.net... etc. that must be bound to the IP of your sending machine... you need this.

  • The sending IP of the machine must as well be bound to the sending domain name with something called a reverse DNS record that can only be added by your ISP (the company providing you with the IP address)

  • The MX record is another type of DNS records that tells e-mail servers / providers where to rout your e-mail messages... You need this as well.

These are the requirements for modern e-mail providers to accept e-mail messages from your machine... or otherwise these e-mails are bounced / rejected.

If you are willing to go this rout and be your own e-mail provider, now you know what to do.

Otherwise, use a relay... please refer to this answer for instructions.

  • I figured out the lines you quote meant postfix was not using a relay. But I did have it configured with line relayhost = [smtp.gmail.com]:587. Evidently, the rest of the configuration was not correct. Now I could work that out, as shown in another answer.. Apr 19, 2021 at 14:13
  • @sancho.sReinstateMonicaCellio l know... you wrote that in your question... Anyway that line was not reflected in your logs and the relayhost was not being used by postfix... I am happy your issue is resolved now :)
    – Raffa
    Apr 19, 2021 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.