$ sudo apt -y install opensmtpd
It can be handy to have your system email you if it detects an issue or potential security risk. Unfortunately, this isn’t always straightforward, especially when you want to send an email from your desktop computer. Sending an email directly from your desktop to your email account is likely going to accomplish nothing. The email will likely be blocked since, to your email provider, it is from an unknown source. I ran into this problem recently trying to set up SmartMonTools to send an email when it detected hard drive errors. The Simple Mail Transfer Protocol (SMTP) is perfect for getting around this by relaying the email through your account from an established email provider.
In my instance, I wanted to relay the alert through my GMail account, which has nice support for SMTP. Using an Ubuntu desktop computer, I figured this would be a breeze. It wasn’t. It turned out to be much harder than I anticipated because many guides demonstrate SMTP relay using antiquated applications, such as SSMTP. Being an OpenBSD fanboy, I with the modern OpenSMTPD application.
This brief guide will walk you through relaying emails on Ubuntu 20.04 with OpenSMTPD 6.6.4.
First, install OpenSMTPD on Ubuntu.
$ sudo apt -y install opensmtpd
This will probably ask you a couple of questions from an ncurses interface, but it should be fairly self explanatory. |
Additionally, you will likely want to install the mailutils
package to provide the standard system mail commands.
$ sudo apt -y install mailutils
The mailutils package is required for the verification step at the end of this tutorial.
|
Grab your email account’s password. For my use case, I was using a GMail account which required two-factor authentication. Because of this, I had to use an App Password instead of my regular password. For any GMail users in a similar situation, instructions on how to generate an App Password can be found here.
The default configuration file only requires a couple of minor tweaks. The gist of the configuration is to relay all mail originating from the local machine to a GMail account.
# $OpenBSD: smtpd.conf,v 1.10 2018/05/24 11:40:17 gilles Exp $ # This is the smtpd server system-wide configuration file. # See smtpd.conf(5) for more information. table secrets file:/etc/mail/secrets (1) table aliases file:/etc/aliases # To accept external mail, replace with: listen on all # listen on localhost action "local" maildir alias <aliases> action "relay" relay host smtp+tls://jdoe@smtp.gmail.com:587 auth <secrets> (2) # Uncomment the following to accept external mail for domain "example.org" # # match from any for domain "example.org" action "local" match for local action "local" match from local for any action "relay"
1 | Use the content of the file /etc/mail/secrets , shown below, for the secrets table. |
2 | The rule to forward all mail to GMail’s SMTP server using TLS. |
Your account credentials must be associated with a label in the /etc/mail/secrets
file.
Create the secrets
file.
$ sudo mkdir /etc/mail
$ sudo touch /etc/mail/secrets
Now, make sure the file’s permissions are tight before putting your plain-text password inside.
$ sudo chmod 640 /etc/mail/secrets (1)
$ sudo chown root:opensmtpd /etc/mail/secrets (2)
1 | Permit read and write access for the file’s owner and read access for the file’s group. |
2 | Set the file’s ownership such that it belongs to the root user and opensmtpd group. |
The following example illustrates the file format.
jdoe jdoe@gmail.com:my_app_password (1)
1 | Use the label jdoe to represent the account jdoe at gmail.com which has the passphrase my_app_password . |
Check that the OpenSMTPD configuration file is valid.
$ smtpd -n
configuration OK
Restart OpenSMTPD to make sure the configuration changes take effect.
$ sudo systemctl restart opensmtpd
Enable OpenSMTPD on system startup, if desired.
$ sudo systemctl enable opensmtpd
To test the configuration, you can send an email from your computer and check if the email appears in the receiving account.
Send a test email using the mail
command from the mailutils
package.
$ echo "Does it work?" | mail -s "OpenSMTPD Test Email" test@example.com
If everything works, you should receive an email at the designated address.