Setup Email Server From Scratch On FreeBSD - 03 Postfix SMTPD
02 FAMP Install <- Intro -> 04 Dovecot IMAP
This tutorial is partially complete 2025-05-14
Postfix, Dovecot, & PostfixAdmin work with MySQL and virtual accounts
can be created with PostfixAdmin and used with an email client. Incoming SPF
milter and outgoing OpenDKIM milter signing tested and working.
#################
# Postfix Setup #
#################
# Postfix on debian has SASL MYSQL etc enabled so use ports to compile.
# I decided not to update ports but just use the version that came with the clean
# install, it is not recommended to mix ports and pkg from different distributions.
# Switch to make to use Maria, I installed mariadb106-server
pkg search mariadb
mariadb106-client-10.6.21 Multithreaded SQL database (client)
mariadb106-server-10.6.21 Multithreaded SQL database (server)
nano /etc/make.conf
DEFAULT_VERSIONS+=mysql=106m
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
# Run make
make
make # again if needed
Just used default selections after this..
After successful compile to make install
make install
# OUTPUT AS FOLLOWS
Adding user 'postfix' to group 'mail'
csshchown: /usr/share/man/man5/mongodb_table.5.gz: No such file or directory
===============================================================
Postfix was *not* activated in /usr/local/etc/mail/mailer.conf!
To finish installation run the following commands:
mkdir -p /usr/local/etc/mail
install -m 0644 /usr/share/postfix/mailer.conf.postfix /usr/local/etc/mail/mailer.conf
===============================================================
To use postfix instead of sendmail:
- clear sendmail queue and stop the sendmail daemons
Run the following commands to enable postfix during startup:
- sysrc postfix_enable="YES"
- sysrc sendmail_enable="NONE"
If postfix is *not* already activated in /usr/local/etc/mail/mailer.conf
- mv /usr/local/etc/mail/mailer.conf /usr/local/etc/mail/mailer.conf.old
- install -d /usr/local/etc/mail
- install -m 0644 /usr/share/postfix/mailer.conf.postfix /usr/local/etc/mail/mailer.conf
Disable sendmail(8) specific tasks,
add the following lines to /etc/periodic.conf(.local):
daily_clean_hoststat_enable="NO"
daily_status_mail_rejects_enable="NO"
daily_status_include_submit_mailq="NO"
daily_submit_queuerun="NO"
If you are using SASL, you need to make sure that postfix has access to read
the sasldb file. This is accomplished by adding postfix to group mail and
making the /usr/etc/sasldb* file(s) readable by group mail (this should
be the default for new installs).
# Follow the instructions above
mkdir -p /usr/local/etc/mail
install -m 0644 /usr/share/postfix/mailer.conf.postfix /usr/local/etc/mail/mailer.conf
# Modify /etc/rc.conf
sysrc postfix_enable="YES"
sysrc sendmail_enable="NONE"
# create the /etc/periodic.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"
# Check postfix has to be in mail group, see if it is correct ...
grep mail /etc/group
mail:*:6:postfix
# We are using sasl mail must be able to read sasldb* so check it too ...
root@okbsd.com:/usr/local/etc# ls -l sasl*
-rw-r----- 1 cyrus mail 16384 May 2 15:03 sasldb2.db
# Looks good so <reboot>
shutdown -r now
root@okbsd.com:~# service postfix status
postfix is running as pid 1892.
# From linuxbabe
root@okbsd.com:~# postconf mail_version
mail_version = 3.9
root@okbsd.com:/usr/ports# sockstat -lp 25 | grep master
root master 1892 13 tcp4 *:25 *:*
root master 1892 14 tcp6 *:25 *:*
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 mx.okbsd.com
250-mx.google.com at your service, [2604:2dc0:100:1261::10]
250-SIZE 157286400
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8
exit
# 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
# Become your user account
su - user
# 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 user@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 - user
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 500MB - 0.5 GB which is quite high
postconf -e message_size_limit=536870912
# Set unlimited mailbox size
root@okbsd.com:/var/mail# postconf | grep mailbox_size_limit
mailbox_size_limit = 51200000
# 0 means unlimited
root@okbsd.com:/var/mail# postconf -e mailbox_size_limit=0
root@okbsd.com:/var/mail# postconf | grep mailbox_size_limit
mailbox_size_limit = 0
# setup hostname
nano /etc/postfix/main.cf
myhostname = mx.okbsd.com
mydomain = okbsd.com
service postfix restart
# Check you alias mapping, alias is no left forwards to account on the right
nano /etc/mail/aliases
# This send all root mail to user@okbsd.com
root: user
# remap the alias map
newaliases
# check inet protocols
root@okbsd.com:/var/mail# postconf inet_protocols
inet_protocols = all
# To only use 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