So you did install Debian on a headless Raspberry Pi? Congratulations!
However, this system lacks one essential thing: mail support.
Many programs in a Debian system use mail to communicate with its human administrator. Some examples:
cronservice mails command execution output to the user who scheduled the command;
- if installed, the unattended-upgrades package mails upgrade reports to the root user, and so does needrestart.
Note that this article is not about handling mail for a domain. It is only about handling mail delivery to local users.
Incidentally, local mail delivery support comes with mail forwarding capability, allowing all mail for root to be sent to a real e-mail address that is (hopefully) actually and frequently read.
Incoming mail processing is the job of a Mail Transport Agent. There are several MTAs in Debian, notably exim, postfix or OpenSMTPd.
This article will describe how to set up local mail handling and forwarding with OpenSMTPd.
Mainly because OpenSMTPd:
- has a much more readable configuration syntax than exim or postfix;
- interfaces easily with other mail components such as an IMAP server or a spam filter;
- handles mail for virtual domains easily.
To be honest, in the present article, only the first point is going to matter. But the last two make is really simple to later add spam filtering or virtual domains.
Before going on, you must ensure your system is up to date.
First, update the list of available packages with this command:
Next, actually update the packages:
Since the updates may include a new kernel, it is best to reboot then reconnect via SSH (and
sudo su again):
Enabling the backports repository
The standard Debian Buster repository contains version 6.0.3 of OpenSMTPd whereas the buster-backports repository has version 6.6.4. Since the modern configuration syntax of OpenSMTPd appeared with version 6.4, we need the buster-backports version.
If you haven’t enabled the buster-backports repository yet, you can refer to this article.
Installing the OpenSMTPd package
This is actually as simple as:
# apt install -t buster-backports opensmtpd
During installation, we will be prompted for several pieces of information. These are, in order:
- System mail name: this is the domain part which OpenSMTPd will add to addresses which do not have one. It defaults to the system’s hostname. Leave it alone! Do not set it to a domain which the system will handle mail for, and do not leave it empty!
- Root and postmaster mail recipient: this should be a mail address which you have access to. Do not enter an address from a domain which the system will handle mail for!
RemarkFor some reason, the initial configuration does not work:
/etc/mailnameis not created with the system mail name, and
/etc/aliasesdoes not contain the root mail forward rule. Running
dpkg-reconfigure opensmtpdfixes both.
That’s it! Now our system can handle mail to the user root and forward it to your real address. Let’s test it:
sendmail -f pi root <<EOT Subject: Mail test Mail test EOT
After the first line is entered, the prompt will switch from “#” to “>”, and remain so until EOT is entered, at which point it will switch to “#” again.
Now go and read the mail for the address you have configured earlier as the root forward recipient. You should see a new mail from pi@systemmailname to root@systemmailname, where systemmailname is, well, the system mail name as you have configured it.