Send system mail Ubuntu 18.04

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 :)


Solution 1:

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    [email protected]: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" [email protected]

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

Solution 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: [email protected]

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.

Solution 3:

I did it with sSMTP as this is a way simpler/smaller than a fully blown postfix setup. (way fewer things to get wrong). Note that this config would only allow for mails outgoing from the machine you are installing it on. it does not suppoer delivery of incoming emails.

This is really a matter for maximum 5min to setup.

For sake of example the following assumptions are true in the below example config:

  • we are sending mail to a local machine in the same network using ip-address on port 2525
  • we are not using encryption or authentication

you can get more details from the man-page, or use google to help you with specific configs (search for: ssnmp+mail+to+gmail)

quick example:

# sudo -i
# apt-get update && apt-get install ssmtp
# nano /etc/ssmtp/ssmtp.conf

Add the following that would fit your environment:

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
[email protected]

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=192.168.2.88:2525

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=hostname.domain

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

test:

# ssmtp [email protected]
To: [email protected]
From: [email protected]
Subject: test email
[enter]
my testing 1,2,3
[ctrl-d]

Added food for thought

I use this to forward the email to a local Docker instance that is converting my email to a Telegram chat message and deliver this in my Telegram group where I bring all my system alerts together from my home automation system to SMNP alerts, and also as per above system emails for local alerts/warnings. Though this is prolly useless to 90% of you reading this, it might remember you all that outgoing system emails might just be the first step of the delivery chain and more can be had from this :)

Enjoy