Setup Email Server From Scratch On FreeBSD #2 - 03 Postfix SMTPD

02 FAMP Install <- Intro -> 04 Dovecot IMAP

We believe in data independence, and support others who want data independence.
This tutorial is complete 2025-08-14 except there is no page for setting up postscreen.

This is version 2 and everthing works.

#############################
# Install and Setup Postfix #
#############################

Postfix packages on FreeBSD do not have all the options we want so we will use ports. It is not advised to mix ports and packages as there may be some incompatibility but I feel it is unlikely mysql apache or php packages will have any effect on Postfix and Dovecot. Some optional plugins have dependencies that can't be installed with ports, so we'll continue use packages for most of the install.

If you do not have ports or want the most recent version, install them, this will take some time.

rm -rf /usr/ports
rm: /usr/ports: Device busy

Ignore the Device busy error.

ls -la /usr/ports
drwxr-xr-x 2 root wheel 2 Aug 6 09:19 ./
drwxr-xr-x 15 root wheel 15 Jun 6 01:34 ../

pkg install portsnap
mkdir -p /var/db/portsnap

cd /usr/ports
portsnap fetch
portsnap extract

For subsequent updates use 'portsnap fetch update'.

Compile Postfix from ports collection.

cd /usr/ports/mail/postfix
make rmconfig
make

Select these, compile will be much faster without PGSQL and SQLITE
X BLACKLISTD
X CDB
X DOCS
X EAI
X INST_BASE
X LDAP
X LMDB
X MYSQL
X PCRE2
PGSQL
X SASL
SQLITE
X TLS

X Native Language Support

After successful compile to make install

make install
pkg lock postfix

Check if sendmail is running

service sendmail status
sendmail is not running.

sysrc postfix_enable="YES"
sysrc sendmail_enable="NONE"
mv /usr/local/etc/mail/mailer.conf /usr/local/etc/mail/mailer.conf.old
install -d /usr/local/etc/mail
install -m 0644 /usr/local/share/postfix/mailer.conf.postfix /usr/local/etc/mail/mailer.conf

nano /etc/periodic.conf
daily_clean_hoststat_enable="NO" daily_status_mail_rejects_enable="NO" daily_status_include_submit_mailq="NO" daily_submit_queuerun="NO"

We are using sasl mail must be able to read sasldb* so check it too ...

root@okbsd.com:~# ls -l /usr/local/etc/sasl*
-rw-r----- 1 cyrus mail 16K Aug 1 14:51 /usr/local/etc/sasldb2.db

Postfix has to be in mail group, see if it is correct ...

root@okbsd.com:#~# grep mail /etc/group
mail:*:6:postfix

If postfix is not in mail add postfix to mail group

pw group mod -n mail -m postfix

Looks good so <reboot>

shutdown -r now

ssh root@okbsd.com

root@okbsd.com:~# service postfix status
postfix is running as pid 3086.

root@okbsd.com:~# postconf mail_version
mail_version = 3.10.1

root@okbsd.com:/usr/ports# sockstat -lp 25 | grep master
root master 3086 13 tcp4 *:25 *:* root master 3086 14 tcp6 *:25 *:*

Check if outbound port 25 is allowed by provider and working.

root@okbsd.com:~# telnet gmail-smtp-in.l.google.com 25
Trying 2607:f8b0:4004:c08::1a... Connected to gmail-smtp-in.l.google.com. Escape character is '^]'. 220 mx.google.com ESMTP d75a77b69052e-48edb679b60si36993221cf.575 - gsmtp > EHLO smtp.okbsd.com 250-mx.google.com at your service, [2604:2dc0:200:187::1] 250-SIZE 157286400 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 <control> - ] quit

Rebuild alias accounts

newaliases

Install Alpine, Alpine is an easy to use mail reader that has a nano like interface Using alpine will make it easy test send and receive email with a subject

pkg install alpine

If you want to add another user use...

adduser

Add a user

pw adduser -n jack -s /usr/local/bin/bash -m

Delete a user

pw userdel -n jack
umount zroot/home/jack
rm -rf /home/jack

Change groupid to 995

pw groupmod -n sudo -g 995

Switch to the user account

su - jack

Open alpine mail reader and send an email to another mail account

alpine
e (exit the greeter)
i (inbox)
c (compose)
ctrl-x (send)
q (quit)

Or use postfix directly as sendmail

echo "test email" | sendmail useremail@gmail.com

Reply to the email from jack@okbsd.com

Find the mail spool directory

root@okbsd.com:/var/spool# postconf mail_spool_directory
mail_spool_directory = /var/mail

Check if mail was recieved

root@okbsd.com:/var/mail# cd /var/mail
root@okbsd.com:/var/mail# ls -l
-rw--w---- 1 root mail 2679 May 5 15:46 root -rw------- 1 user user 5354 May 5 15:53 user

root@okbsd.com:/var/mail# cat user
<raw mail output>

Read with alpine

su - jack
alpine

If you have problems sending or receiving mail check the mail log

tail -300 -f /var/log/maillog

Change message size limit

postconf | grep message_size_limit
message_size_limit = 10240000

Increase limit to 50MB

postconf -e message_size_limit=52428800

Increase limit to 100MB

postconf -e message_size_limit=104857600

Increase limit to 200MB

postconf -e message_size_limit=209715200

Increase limit to 500MB - 0.5 GB which is quite high

postconf -e message_size_limit=536870912

Set unlimited mailbox size

postconf | grep mailbox_size_limit
mailbox_size_limit = 51200000

0 means unlimited

postconf -e mailbox_size_limit=0
postconf | grep mailbox_size_limit
mailbox_size_limit = 0

Setup hostname, we are going to be editing this file alot.

cp /usr/local/etc/postfix/main.cf /usr/local/etc/postfix/main.cf._default_
cp /usr/local/etc/postfix/master.cf /usr/local/etc/postfix/master.cf._default_

nano /usr/local/etc/postfix/main.cf
myhostname = smtp.okbsd.com mydomain = okbsd.com

service postfix restart
service postfix status

Check you alias mapping, alias is no left forwards to account on the right

This send all root mail to user jack@okbsd.com

nano /etc/mail/aliases
root: jack

Remap the aliases

newaliases

check inet protocols

postconf inet_protocols
inet_protocols = all

If your host only supports ipv4, set ...

postconf -e "inet_protocols = ipv4"
service postfix restart

To use both ipv4 and ipv6...

postconf -e "inet_protocols = all"
service postfix restart

####################################
# Next install dovecot IMAP server #
####################################

02 FAMP Install <- Intro -> 04 Dovecot IMAP