4

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
    

Notes:

  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.

Related:

  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 '21 at 13:29
  • I recommend nullmailer it's orders of magnitude easier to set up than postfix. – Organic Marble Apr 18 '21 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. – sancho.s ReinstateMonicaCellio Apr 18 '21 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. – sancho.s ReinstateMonicaCellio Apr 18 '21 at 13:38
  • Indepth postfix tutorial here: https://arstechnica.com/information-technology/2014/02/how-to-run-your-own-e-mail-server-with-your-own-domain-part-1/2/ – Organic Marble Apr 18 '21 at 13:39
  • No, nullmailer cannot receive. – Organic Marble Apr 18 '21 at 13:41
  • @OrganicMarble - If I understand correctly, the in-depth guide you linked states as a requirement that "all you need is a registrar and a credit card" for a domain. That is exactly what I mean to avoid, at this point. If that is even possible, and I guess the answer is YES, that is possible. So I guess that guide would not apply to my needs. – sancho.s ReinstateMonicaCellio Apr 18 '21 at 13:44
  • To use postfix to send email "this option requires you have a fully qualified domain name and full access to modify DNS records for this domain name as this method requires adding a MX record and a reverse DNS for it to send and receive emails." If you know a way to get a fqdn and dns record modification access without paying for a domain, then you don't need to pay for a domain. (I am not trying to be snarky, I ran screaming from postfix after reading that tutorial) Quote from here https://askubuntu.com/a/1332112/243321 – Organic Marble Apr 18 '21 at 13:47
  • The other methods in that answer don't require a domain, but you have to have an outside email provider. So why go through the pain of postfix setup if you need another provider anyway? nullmailer for the win (for my applications, anyway) – Organic Marble Apr 18 '21 at 13:51
  • @OrganicMarble - Why postfix setup+gmail? Because I guessed (I may be wrong) having it would provide more flexibility for automation, scripting, etc., and then channeling emails through my gmail account. Why not nullmailer? Being inexperienced, I have no preference, other than the stated possibility of needing to receive emails in the future. – sancho.s ReinstateMonicaCellio Apr 18 '21 at 13:59
  • There's absolutely nothing wrong with postfix, I just wasn't up to the challenge and I didn't need to go through it. – Organic Marble Apr 18 '21 at 14:00
  • As @OrganicMarble mentions, to create a standalone presence, you need a FQDN. It's much easier to set a relayhost and relay_domains. relayhost = smtp.gmail.com:587 and relay_domains = gmail.com. You'll also have to go through the pain of setting up TLS and SASL to authorize to your gmail account. See the docs in /usr/share/doc/postfix/TLS_README.gz for additional help. – heynnema Apr 18 '21 at 14:18
  • I agree with @OrganicMarble. If you don't need to have your own email address likesancho@sreinstatemonicacellio.com and don't want that address forward to Gmail, but reach your computer directly, then you you don't need postfix. – user68186 Apr 18 '21 at 14:26
  • @user68186 - I understand I don't need postfix for my needs. Two questions follow then:
    1. What would another option be? (if I want send-only, and send-receive)
    2. Am I correct in thinking that, even if not needed, postfix enhances automation/scripting capability?
    – sancho.s ReinstateMonicaCellio Apr 18 '21 at 14:42
  • @heynnema - I don't know what "a standalone presence" is. If that is an email like sancho@sreinstatemonicacellio.com (comment by @user68186) then I don't need that, as mentioned in the OP.

    I am not aware of the pains you cite. If you know of any guide, that would be great. I thought the links I read and posted would be enough (which were not, or were misinterpreted). In addition, two questions follow then:

    1. What would another option be? (if I want send-only, and send-receive)
    2. Am I correct in thinking that, even if not needed, postfix enhances automation/scripting capability?
    – sancho.s ReinstateMonicaCellio Apr 18 '21 at 14:46
  • By "standalone presence" I mean a complete registered Internet site with a FQDN... like google.com or ebay.com. If you sudo dpkg-reconfigure postfix it's one of the first questions that it asks. Have you by chance checked out the docs at /usr/share/doc/postfix/? There's a ton there. I know that I had to spend a lot of time setting up Postfix, and I personally only cared about being able to send emails. – heynnema Apr 18 '21 at 15:05
  • To answer your 2 questions: 1. Some of us commenting here gave you other options like nullmailer and msmtp. 2. I use msmtp to send emails using bash script and cron. I don't think the automatic capability is any less or more than postscript. – user68186 Apr 18 '21 at 16:38
  • @heynnema - It actually took significant tinkering and reading to make it work, but the process itself is not particularly complex. The only problem is that there is no step-by-step guide (that I could find), showing the minor but crucial differences with other configurations. And after learning this, I also managed to configure Yahoo. – sancho.s ReinstateMonicaCellio Apr 19 '21 at 04:03

2 Answers2

4

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:

relay=local

and

relay=none

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.

Raffa
  • 32,237
  • 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.. – sancho.s ReinstateMonicaCellio Apr 19 '21 at 14:13
  • 1
    @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 '21 at 16:32
4

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 '21 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. – sancho.s ReinstateMonicaCellio Apr 19 '21 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 '21 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. – sancho.s ReinstateMonicaCellio Apr 19 '21 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 '21 at 14:18
  • I followed all steps but I keep getting an error <my_email@gmail.com>: unknown user: "my_email" \ Diagnostic-Code: X-Postfix; unknown user: "my_email". This is the recipient address! – Enissay Nov 01 '21 at 00:20