Carlos E. Garcia

A simple relay via OpenSMTPD

02 Mar 2017, updated: 24 Sept 2022

Using OpenSMTPD as a relay through your favorite email service provider is quite an easy setup. The first step is to of course install OpenSMTPD and OpenSMTPD-extras. Do make sure to go through the manual pages, quite informative and useful.

The second thing we will need are the credentials to the email service provider which are referenced under OpenSMTPD's configuration file. The credentials file /etc/mail/secrets holds the user name and password, along with a label of your choosing.

# touch /etc/mail/secrets
# chmod 640 /etc/mail/secrets
# chown root:_smtpd /etc/mail/secrets
# echo "bob username:password" > /etc/mail/secrets
The final step would be to edit OpenSMTPD's main configuration smtpd.conf file like so:
# Table setup
table aliases file:/etc/mail/aliases
table secrets file:/etc/mail/secrets

# To accept external mail, replace with: listen on all
listen on lo0

action "local_mail" mbox alias 
action "outbound" relay host smtp+tls://bob@smtp.example.com \
        auth 

# Uncomment the following to accept external mail for domain
# "example.org"
# match from any for domain "example.org" action "local_mail"
match from local for local action "local_mail"
match from local for any action "outbound"

To make sure there are no syntax issues in the configuration file, run the following command:

# smtpd -n
configuration OK

We then restart smtpd to pick up the new configuration. Command below depends on what system you're running. OpenBSD system would be:

# rcctl restart smtpd
smtpd(ok)
smtpd(ok)

You may check to make sure it is working through the terminal with the mail command:

$ echo "Message" | mail -s "Subject" to@example.com

One last tip depending on your needs, do make sure the GECOS field is properly configured for your local account.