Published on December 29, 2020
Installation
Install postfix
:
apk add postfix postfix-pgsql postfix-pcre
Configuration
Create the mail directory and assign vmail as the owner:
mkdir -p /var/mail/domains
chown -R vmail:postdrop /var/mail/domains
Get the uid and gid of the vmail
user (in our case the uid is 105 and the gid is 107):
grep vmail /etc/passwd
Edit /etc/postfix/main.cf
:
inet_protocols = ipv4 ipv6
myhostname = example.com
mydomain = example.com
relayhost =
mynetworks = 127.0.0.0/8
mydestination =
mydestination = localhost.$mydomain, localhost
mynetworks_style = subnet
mynetworks = 127.0.0.0/8
virtual_mailbox_domains = proxy:pgsql:/etc/postfix/sql/pgsql_virtual_domains_maps.cf
virtual_alias_maps = proxy:pgsql:/etc/postfix/sql/pgsql_virtual_alias_maps.cf,
proxy:pgsql:/etc/postfix/sql/pgsql_virtual_alias_domain_maps.cf,
proxy:pgsql:/etc/postfix/sql/pgsql_virtual_alias_domain_catchall_maps.cf
virtual_mailbox_maps = proxy:pgsql:/etc/postfix/sql/pgsql_virtual_mailbox_maps.cf,
proxy:pgsql:/etc/postfix/sql/pgsql_virtual_alias_domain_mailbox_maps.cf
virtual_mailbox_base = /var/mail/domains/
virtual_gid_maps = static:107
virtual_uid_maps = static:105
virtual_minimum_uid = 100
virtual_transport = lmtp:unix:private/dovecot-lmtp
mailbox_transport = virtual
local_transport = virtual
local_transport_maps = $virtual_mailbox_maps
smtpd_helo_required = yes
disable_vrfy_command = yes
message_size_limit = 104857600
virtual_mailbox_limit = 104857600
queue_minfree = 51200000
smtpd_sender_restrictions =
permit_mynetworks,
reject_non_fqdn_sender,
reject_unknown_sender_domain
smtpd_recipient_restrictions =
reject_non_fqdn_recipient,
reject_unknown_recipient_domain,
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
reject_rbl_client dnsbl.sorbs.net,
reject_rbl_client zen.spamhaus.org,
reject_rbl_client bl.spamcop.net
smtpd_data_restrictions = reject_unauth_pipelining
smtputf8_enable = no
broken_sasl_auth_clients = no
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain =
smtpd_sasl_authenticated_header = no
Adjust the password accordingly and run the following sequence of commands to create the PostgreSQL scripts for Postfix to access the Postfix database:
cd /etc/postfix
mkdir sql
PASSWORD="ChangeMe"
cat - <<EOF >sql/pgsql_virtual_alias_domain_catchall_maps.cf
user = postfix
password = $PASSWORD
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' AND alias.address = '@' || alias_domain.target_domain AND alias.active = true AND alias_domain.active = true
EOF
cat - <<EOF >sql/pgsql_virtual_alias_domain_mailbox_maps.cf
user = postfix
password = $PASSWORD
hosts = localhost
dbname = postfix
query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' AND mailbox.username = '%u' || '@' || alias_domain.target_domain AND mailbox.active = true AND alias_domain.active
EOF
cat - <<EOF >sql/pgsql_virtual_alias_domain_maps.cf
user = postfix
password = $PASSWORD
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' AND alias.address = '%u' || '@' || alias_domain.target_domain AND alias.active = true AND alias_domain.active = true
EOF
cat - <<EOF >sql/pgsql_virtual_alias_maps.cf
user = postfix
password = $PASSWORD
hosts = localhost
dbname = postfix
query = SELECT goto FROM alias WHERE address = '%s' AND active = '1'
EOF
cat - <<EOF >sql/pgsql_virtual_domains_maps.cf
user = postfix
password = $PASSWORD
hosts = localhost
dbname = postfix
query = SELECT domain FROM domain WHERE domain = '%s' AND active='1'
EOF
cat - <<EOF >sql/pgsql_virtual_mailbox_maps.cf
user = postfix
password = $PASSWORD
hosts = localhost
dbname = postfix
query = SELECT maildir FROM mailbox WHERE username = '%s' AND active = true
EOF
chown -R postfix:postfix sql
chmod 640 sql/*
Allow TCP traffic on the SMTP port (port 25):
ufw allow smtp
Check whether the Postfix configuration is valid:
postconf -n
Start the Postfix service:
newaliases
/etc/init.d/postfix start
rc-update add postfix
Configuring DNS
Add the following DNS records:
@ MX 10 mail.example.com.
If you like my work or if my work has been useful to you in any way, then feel free to donate me a cup of coffee. Any donation is much appreciated!