OpenSMTPD relay setup

These days, most of us don't run a mail server to send e-mail. Instead, we deliver to a provider's mail server via SMTP.

Setting this up in OpenSMTPD is quite simple. We will assume that all of the machine's e-mail is going to be relayed through a single GMail account. A more complex situation will require a more complex setup.

This is based on the EXAMPLES section in OpenSMTPD's manual.

In general (particularly if you're using multi-factor authentication), you need to set up an »app password« for your Google account. The 16 character password must be stored in an arbitrary file, we'll call it /etc/mail/secrets, giving it an arbitrary »label« for which we'll use gmail.

# touch /etc/mail/secrets
# chmod 640 /etc/mail/secrets
# chown root:_smtpd /etc/mail/secrets
# echo "gmail username:password" > /etc/mail/secrets

Next, the following line needs to be added to /etc/mail/smtpd.conf:

table secrets file:/etc/mail/secrets

Then, the relay action of /etc/mail/smtpd.conf needs to be adjusted as follows. We're using the same »label« gmail that we used in the secrets file.

action "relay" relay \
        host smtps://gmail@smtp.gmail.com \
        auth <secrets>

You should then restart smtpd.

# rcctl restart smtpd

Last, in order to provide a proper »From« line, you should add a line like the following to ~/.mailrc:

set from="User Name <username@gmail.com>"

It's now time to send your first e-mail, preferably to yourself:

$ echo hi | mail -s test0 username@gmail.com

If it gets stuck, look at /var/log/maillog for the error. While your e-mail is in the queue, OpenSMTPD will automatically attempt to deliver it after every change to the configuration. Use smtpctl show queue and smtpctl remove to control the mail queue.