I’m using Ubuntu 16.04 LTS and I want to use the command line to send messages to my regular email account. There are administrative tasks that sends mail to root / administrator but to simplify my tasks I want these messages to be sent to my regular email account in Gmail.
I installed postfix first.
#apt install postfix mailutils
Then accepted some basic configurations that were prompted when installing postfix.
General type of mail configuration?: Internet Site
System mail name: a hostname that will be used in mail headers, a FQDN is preferred, desktop, home
Root and postmaster mail recipient: root
Other destinations to accept mail for: $myhostname, desktop, localhost.localdomain, localhost
Force synchronous updates on mail queue?: No
Local networks: 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.0.0/24
Mailbox size limit: 0
Local address extension character: +
Internet protocols to use: all
If you need to return to these settings, type:
You need to create a password file which Postfix will use to authenticate with Gmail. In Ubuntu such a file should be placed in /etc/postfix/sasl/sasl_passwd. So I used my favorite text editor and:
“[smtp.gmail.com]:587 emailaddr:app password” – do this without the quotes, okay.
Save and close the file.
Protect this file.
#chmod 600 /etc/postfix/sasl/sasl_passwd
Make sure the following entries are present in /etc/postfix/main.cf so open the file and add or modify the file.
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
Use postmap to compile and hash the contents of /etc/postfix/sasl/sasl_passwd. So type:
Then restart postfix by typing:
#systemctl restart postfix.service
That’s it. You can check your work by sending a test email.
$echo -e “Hello. This is a test message.”|mail -s “Test Message” username