4

Hello I have looked at many different guides on the web for how to send system mail to my gmail from ubuntu 18.04 in case of problems on the device / server - But can't find a simple guide that i think makes sense? Someone who has some advice?

I don't want to set up a mail server but keep it as simple as possible when I'm a beginner. If I could use google smtp server to do that it would be an advantage rather than having to mess with my own domain :)

5

Install the packages needed to get a basic system for handling mail:

sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules

Next modify (as root) the configuration file /etc/postfix/main.cf to have something like this:

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CApath = /etc/ssl/certs
smtpd_tls_CApath = /etc/ssl/certs
smtp_use_tls = yes

Next create/modify (as root) the /etc/postfix/sasl_passwd to contain:

[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

Make sure the permissions for the file are correct or it might get mad:

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Lastly reload postifx:

sudo /etc/init.d/postfix reload

Now you can run a test:

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

You should get an email that comes from the SMTP account you configured.

|improve this answer|||||
  • Thank you :) How do i set it to send local output - or if you did it in your guide above how it works :)? – Rene Mortensen Jan 25 '19 at 14:31
  • I have followed the above but do not get any mail in my inbox when I run the command: echo "Test mail thingy" | mail -s "Test Postfix Subject" rmortensen84@gmail.com – Rene Mortensen Jan 25 '19 at 16:03
  • You'll want to look at /var/log/mail.log for why the send failed. If it says SSL authentication failed you might have to go to this URL to authorize your "device" google.com/accounts/DisplayUnlockCaptcha – Kristopher Ives Jan 25 '19 at 17:56
  • It's also possible that if it says SSL error it's because /etc/ssl/certs doesn't contain the certificate for Gmail, although I'm not certain why, but I have read you may also need to set smtpd_tls_CApath which I have added to the config example in this answer. – Kristopher Ives Jan 25 '19 at 17:58
2

In order to send local (system) email to an external email address, in addition to installing a mail handling system as proposed in the currently accepted answer, it is necessary to modify the /etc/aliases file. This file is read by the mail system to determine the final recipient of all internal mail (such as generated by cron jobs or other system errors). A suggested /etc/aliases file is below:

# /etc/aliases
mailer-daemon: postmaster
postmaster: root
nobody: root
hostmaster: root
usenet: root
news: root
webmaster: root
www: root
ftp: root
abuse: root
noc: root
security: root
root: user
user: username@gmail.com

where user and username should be replaced by the appropriate entries for your system. This file redirects all email sent to all of the standard pseudo-users to the root user, and then the last two lines redirect emails to root to the local real user, and then finally externally to gmail.

Once the /etc/aliases file is updated, it is necessary to run the command

sudo newaliases

in order to get the system to notice the changes.

|improve this answer|||||

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.