Setup Email Server From Scratch Debian #2 - 03 Postfix SMTPD

02 LAMP Install <- Intro -> 04 Dovecot IMAP

We believe in data independence, and support others who want data independence.
Debian Email From Scratch version 2 finished 2025-07-30.

We are still adding to it but it all works!

#################
# Postfix Setup #
#################

apt-get -y install postfix

Choose: Internet Site
System mail name: okdeb.com

systemctl reload postfix

postconf mail_version
mail_version = 3.7.11

systemctl status postfix

root@okdeb.com:~# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
     Loaded: loaded (/lib/systemd/system/postfix.service; enabled; preset: enabled)
     Active: active (exited) since Wed 2025-07-16 11:16:32 PDT; 13min ago
       Docs: man:postfix(1)
    Process: 1011 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 1011 (code=exited, status=0/SUCCESS)
        CPU: 1ms

Jul 16 11:16:32 okdeb.com systemd[1]: Starting postfix.service - Postfix Mail Transport Agent...
Jul 16 11:16:32 okdeb.com systemd[1]: Finished postfix.service - Postfix Mail Transport Agent.

ss -lnpt | grep master
LISTEN 0   100   0.0.0.0:25    0.0.0.0:*    users:(("master",pid=4301,fd=13))
LISTEN 0   100      [::]:25       [::]:*    users:(("master",pid=4301,fd=14))

# Check if the smtp port is open on your server

nmap -T4 -F mx.okdeb.com
PORT     STATE  SERVICE
25/tcp   open   smtp
80/tcp   open   http
443/tcp  open   https

# Check if outbound smtp is allowed

root@okdeb.com:~# apt install telnet
root@okdeb.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.okdeb.com
250-mx.google.com at your service, [2604:2dc0:202:300::3645]
250-SIZE 157286400
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8

^] to quit

# Check Aliases

nano /etc/aliases

# Rebuild Aliases

newaliases

# Send a test email to your working email account. Sending to a gmail address 
# worked but it was in my spam. I tried sending to my zimbra email server but 
# if failed due to hostname misconfiguration. I was planning to move the zimbra 
# accounts to this new server and had added the zimbra hostname in /etc/hosts 
# causing a mail loop error. I fixed the hostname issue in /etc/hosts and I was 
# then able to send to a test email to my zimbra account.

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

# Once you are able to send email reply to the email from your other account.

# 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

apt install alpine
alpine
e
L
# You should be able to read the email using alpine.
q
y

# Add a user account, change user to any name you wish to use or just use the debian
# account or any other account created during install. I called this user jack and
# added this user to group sudo. It is good to add a test user and admin account, you
# could use root as the admin account or setup another account admin or any name
# of your choice.

useradd -m -s /usr/bin/bash -G sudo jack
passwd jack

# Check sudo with visudo

visudo

# Look for this line
# ---
%sudo   ALL=(ALL:ALL) ALL
# ---

# Switch to the use account

root@okdeb.com:~# su - jack
jack@okdeb.com:~$

# 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

# Exit the user account

jack@okdeb.com:~$ exit
root@okdeb.com:~#

# Reply to the email from jack@okdeb.com

# Find the mail spool directory
root@okdeb.com:/var/spool# postconf mail_spool_directory
mail_spool_directory = /var/mail

# Check if mail was recieved
root@okdeb.com:/var/mail# cd /var/mail
root@okdeb.com:/var/mail# ls -l
-rw--w----  1 root       mail       2679 May  5 15:46 root
-rw-------  1 jack       jack       5354 May  5 15:53 jack

root@okdeb.com:/var/mail# cat jack
<raw mail output>

# Change to the jack account and read with alpine

su - jack
alpine
exit

# If you have problems sending or receiving mail check the mail log
journalctl
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@okdeb.com:/var/mail# postconf | grep mailbox_size_limit
mailbox_size_limit = 51200000

# 0 means unlimited
root@okdeb.com:/var/mail# postconf -e mailbox_size_limit=0
root@okdeb.com:/var/mail# postconf | grep mailbox_size_limit
mailbox_size_limit = 0

# Open postfix main.cf file and change myhostname to the full hostname and add mydomain

nano /etc/postfix/main.cf
# ---
myhostname = mx.okdeb.com
mydomain = okdeb.com
# ---

root@okdeb.com:~# systemctl restart postfix
root@okdeb.com:~# systemctl status postfix

# Check you alias mapping, alias is no left forwards to account on the right
# This send all root mail to jack@okdeb.com

nano /etc/mail/aliases
# ---
root:	jack
# ---

# remap the alias map
newaliases

# check inet protocols
root@okdeb.com:/var/mail# postconf inet_protocols
inet_protocols = all

# okdeb.com has ipv6 with OVH so I didn't do this but if you only want to use 
# ipv4 or if ipv6 is not available with your provider then disable it with ...

postconf -e "inet_protocols = ipv4"
service postfix restart

# To use both ipv4 and ipv6...
postconf -e "inet_protocols = all"
service postfix restart

# To Upgrade Postfix select : No configuration to prevent overwriting your
# configuration files.

apt update
apt upgrade

# Next install dovecot IMAP server

02 LAMP Install <- Intro -> 04 Dovecot IMAP