DEV Community

Cover image for OpenSMTPD: How to receive emails sent by remote hosts to OS local users
nabbisen
nabbisen

Posted on • Updated on

OpenSMTPD: How to receive emails sent by remote hosts to OS local users

* The cover image is originally by Foto-Rabe and edited with great appreciation.


Summary

OpenSMTPD is a free implementation of the Simple Mail Transfer Protocol aka SMTP, which is developed by The OpenBSD Project.
It is available as daemon in not only BSD operating systems such as FreeBSD and macOS but also Linux distributions. For example, Arch Linux and Debian have their own manual pages about OpenSMTPD: Arch Linux's, Debian's.

This post shows how to receive emails from remote hosts, other service, to your server with OpenSMTPD for quick use of it.
What is necessary is a little configuration of DNS (A/MX) records and smtpd.conf in /etc/mail, and a few commands.

Environment

  • OS: OpenBSD 6.7
  • SMTP Server: OpenSMTPD 6.7

Tutorial

  1. Configure DNS server
  2. Edit smtpd.conf
  3. Set a local user (optional)
  4. Receive messages and read them
✿ ✿ ✿

Configure DNS server

First, access your DNS server or service, and add A/MX records pointing to your domain (named $DOMAIN here).
As a side note, a MX record was not mandatory in my case when the $DOMAIN was completely unique.

Edit smtpd.conf

You can find the original configuration file named smtpd.conf in /etc/mail.

# nvim /etc/mail/smtpd.conf
Enter fullscreen mode Exit fullscreen mode

Add just two lines to it:

  [...]
  table aliases file:/etc/mail/aliases

  listen on socket
  [...]
  listen on lo0
+ listen on egress
  [...]
  action "local_mail" mbox alias <aliases>
  [...]
  match from local for local action "local_mail"
  match from local for any action "outbound"
+ match from any for domain "$DOMAIN" action "local_mail"
Enter fullscreen mode Exit fullscreen mode

The first listen on egress enables egress traffic to let the daemon to listen on external IP addresses. Alternatively, listen on all may be used: all = lo0 + egress.
The second and the last match statement declares messages for $DOMAIN will be dealed with by the "local_mail" action and written to mbox files. Besides, for domain "$DOMAIN" in it is an option for safety. It also works with for any instead.

Restart smtpd daemon after changing smtpd.conf:

# rcctl restart smtpd
Enter fullscreen mode Exit fullscreen mode

Set a local user (optional)

If you are supposed to accept messages for root, this section is skippable.
You can receive messages for "root@$DOMAIN".
Additionally, "postmaster@$DOMAIN", "operator@$DOMAIN" and so on are able to accept their messages. They will be found in root's mbox.
This is because these settings are in the default aliases file in /etc/mail:

postmaster: root
[...]
operator: root
www:    root
Enter fullscreen mode Exit fullscreen mode

Well, when you want to receive messages for a local user, edit /etc/mail/aliases, which is used as aliases table in smtpd.conf. It is the database file and used in the "local_mail" action to do mbox alias <aliases>.

Add a line to the bottom of:

# nvim /etc/mail/aliases
Enter fullscreen mode Exit fullscreen mode

in this way:

  [...]
+ someuser: someuser
Enter fullscreen mode Exit fullscreen mode

The left value is alias name and the right is the destination.

Then, update the database:

# newaliases
/etc/mail/aliases: 69 aliases
Enter fullscreen mode Exit fullscreen mode

There is no need to restart smtpd daemon at changing only aliases.
Now you can receive messages for someuser@$DOMAIN.

Besides, you may add a user with adduser, of course, to use as an email receiver beforehand.

Receive messages and read them

Let's send an email with your Gmail or another service to root@$DOMAIN, postmaster@$DOMAIN or someuser@$DOMAIN.
By the way, execute doas su - to act as root if necessary.
Then run mail:

# mail
Mail version 8.1.2 01/15/2001.  Type ? for help.
"/var/mail/root": 1 message 1 new
>N  1 root@$DOMAIN  Fri Jun  5 18:22   44/2379  subject
&
Enter fullscreen mode Exit fullscreen mode

The messages will be listed.
In this interactive shell, various [command]s are available such as [?] for [help], go to the [n]ext message, [r]eply, et al.

Top comments (0)