DEV Community

Cover image for How to send and receive emails with OpenSMTPD
manuel
manuel

Posted on • Originally published at wildauer.io on

How to send and receive emails with OpenSMTPD

Populate /etc/mail/aliases

echo "root: YOUR_LOCAL_USER" >> /etc/mail/aliases
Enter fullscreen mode Exit fullscreen mode

That means that all emails sent to root are going to YOUR_LOCAL_USER.

If you want you can add some other aliases, like “contact” or “hi”.

echo "contact: YOUR_LOCAL_USER" >> /etc/mail/aliases
echo "hi: YOUR_LOCAL_USER" >> /etc/mail/aliases
Enter fullscreen mode Exit fullscreen mode

After we add all our aliases it’s necessary to run newaliases.

newaliases
/etc/mail/aliases: 70 aliases
Enter fullscreen mode Exit fullscreen mode

Now you should create an SSL certificate like here or use an existing one.

Now we can populate /etc/mail/smtpd.conf and replace example.com with your domain. Everything we want is to receive emails from other for local users and deliver all emails to users mbox => /var/mail/YOUR_LOCAL_USER

table aliases file:/etc/mail/aliases

pki example.com key "/path/to/your/example.com.key"
pki example.com certificate "/path/to/your/exmaple.com.crt"

listen on egress tls pki exmaple.com
listen on egress port 587 hostname example.com tls-require pki example.com auth mask-source

accept from any for domain "example.com" alias <aliases> deliver to mbox
accept from any for local alias <aliases> deliver to mbox
accept from local for any relay
Enter fullscreen mode Exit fullscreen mode

That’s it. OpenSMTPD is listen on port 25 and 587 and accept encrypted connections. The key and certificate location are configured with the pki keyword. And the messages are delivered to system mbox of the user (/var/mail/YOUR_LOCAL_USER).

Populate /etc/mail/mailname with your domain.

echo "example.com">/etc/mail/mailname
Enter fullscreen mode Exit fullscreen mode

Now you can check you configuration smtpd -n and when everything looks ok, you can restart OpenSMTPD rcctl restart smtpd

Check your OpenSMTPD server with telnet

telnet exmaple.com

EHLO exmaple.com
MAIL FROM: <from@somehwere.tld>
RCPT TO: <to@example.com>
DATA
Subject: This is just a test
<- blank line ->
This is a test :)
<- blank line ->
.
QUIT
Enter fullscreen mode Exit fullscreen mode

If it’s says something like 250 2.0.0: 574eff74 Message accepted for delivery, congratulation OpenSMTPD works.

Top comments (0)