Simple Mailserver With Postfix, Dovecot, And Sieve On FreeBSD 10

Updated on November 21, 2023
Simple Mailserver With Postfix, Dovecot, And Sieve On FreeBSD 10 header image

This tutorial will show you how to get a simple mail server on FreeBSD 10, with Postfix as MTA, Dovecot as MDA and Sieve for sorting mail - all over an encrypted connection for improved security.

Prerequisites

Installation Steps

In order to configure everything properly, first install these packages:

pkg
pkg update -f
pkg install dovecot dovecot-managesieve postfix

When pkg asks you:

Would you like to activate Postfix in /etc/mail/mailer.conf [n]?

Answer with y.

The first configuration step is done in /usr/local/etc/dovecot.conf. In the protocol lda section, we will add a postmaster address so that people can contact you in case of a failure. Next, we will allow auto-creation of folders and auto-subscription of said folders to avoid an inconsistent state between your mail client and the server:

postmaster_address = yourname@yourdomain.tld
lda_mailbox_autocreate = yes
lda_mailbox_autosubscribe = yes

The next step is to assign the correct path for your users' mailboxes in the same file, under the mail_location directive:

mail_location = maildir:~/Maildir

If the line already exists, comment it out and replace it with the one above. Now to configure encryption for IMAP. I assume that you already have a certificate in place:

ssl = yes
ssl_cert = </path/to/your/certificate
ssl_key = </path/to/your/key

The last step is to tell Postfix to deliver the mails via Dovecot, and also tell it to use SASL via Dovecot for authentication. Add the following lines to /usr/local/etc/postfix/main.cf:

mailbox_command = /usr/local/libexec/dovecot/deliver
smtpd_use_tls = no
smtpd_sasl_auth_enable = yes
smtpd_sasl_path        = private/auth
smtpd_sasl_type        = dovecot
smtpd_recipient_restrictions =
  permit_mynetworks,
  permit_sasl_authenticated,
  reject_unauth_destination

Next, add both services to /etc/rc.conf and start them:

echo "postfix_enable=YES" >> /etc/rc.conf
echo "dovecot_enable=YES" >> /etc/rc.conf
/usr/local/etc/rc.d/postfix start
/usr/local/etc/rc.d/dovecot start

Since it is considered rude to use the root-account for mailing, you should create a separate user for your mailing needs:

 pw user add youruser -m
 passwd youruser

Inform postfix about the new user:

newaliases

Now you can test the mail functionality with the following command:

echo "TEST" | mail -s "testmail" youruser@localhost && tail -f /var/log/maillog

If your log files contain a line similar to the following one (The last part is the important) ..

postfix/local[27114]: 3F63C5B71: to=<youruser@localhost>, orig_to=<youruser@localhost>, relay=local, delay=0.01, delays=0/0/0/0.01, dsn=2.0.0, status=sent (delivered to command: /usr/libexec/dovecot/deliver)

.. then everything is working properly.

Sieve is automatically installed and configured by the dovecot-managesieve package itself.

And that's it. You can now log in via IMAP or POP3 in a secure fashion, send transport encrypted mails, and write filters with Sieve.

Happy mailing!

Other versions

Debian 7

CentOS 7

FreeBSD 10