OpenSMTPD: How to receive emails sent by remote hosts to OS local users
Tags: opensmtpd, openbsd, email, server
Published: 2020-06-18 07:03:47

<small>* The cover image is originally by [Foto-Rabe](https://pixabay.com/users/foto-rabe-715168/) and edited with great appreciation.</small>

---

## Summary

[OpenSMTPD](https://www.opensmtpd.org) is a free implementation of the [Simple Mail Transfer Protocol](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) aka SMTP, which is developed by The [OpenBSD](https://www.openbsd.org) Project.
It is available as daemon in not only BSD operating systems such as [FreeBSD](https://www.freebsd.org) and [macOS](https://www.apple.com/macos) but also Linux distributions. For example, [Arch Linux](https://www.archlinux.org/) and [Debian](https://www.debian.org) have their own manual pages about OpenSMTPD: [Arch Linux's](https://wiki.archlinux.org/index.php/OpenSMTPD), [Debian's](https://manpages.debian.org/testing/opensmtpd/smtpd.conf.5.en.html).

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](https://en.wikipedia.org/wiki/Domain_Name_System) (A/[MX](https://en.wikipedia.org/wiki/MX_record)) [records](https://en.wikipedia.org/wiki/List_of_DNS_record_types) and [`smtpd.conf`](https://man.openbsd.org/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

<center>✿ ✿ ✿</center>

### 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`.

```console
# nvim /etc/mail/smtpd.conf
```

Add just two lines to it:

```diff
  [...]
  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"
```

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](https://en.wikipedia.org/wiki/Mbox) files. Besides, `for domain "$DOMAIN"` in it is an option for safety. It also works with `for any` instead.

Restart [smtpd](https://man.openbsd.org/smtpd.8) daemon after changing `smtpd.conf`:

```console
# rcctl restart smtpd
```

### 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`](https://man.openbsd.org/aliases) file in `/etc/mail`:

```config
postmaster: root
[...]
operator: root
www:    root
```

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:

```console
# nvim /etc/mail/aliases
```

in this way:

```diff
  [...]
+ someuser: someuser
```

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

Then, update the database:

```console
# newaliases
/etc/mail/aliases: 69 aliases
```

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`](https://man.openbsd.org/adduser.8), 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`](https://man.openbsd.org/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
&
```

The messages will be listed.
In this interactive shell, various [\[command\]s](https://man.openbsd.org/mail#SUMMARY) are available such as [?] for [help], go to the [n]ext message, [r]eply, et al.