E-Mail/Server/Setup/Postfix

Aus Foxwiki

Postfix

Postfix is a Mail Transfer Agent (MTA) that relays mail between your Server and the internet.

  • It is highly configurable, allowing for great flexibility.
  • This guide maintains many of Posfix’s default configuration values.

How Do Postfix And Dovecot Work Together?

Postfix and Dovecot work together to help make email servers function as intended (sending and receiving email) and accessible to whichever email client the end user wishes to use.

  • While Postfix manages email delivery and receipt, Dovecot allows email clients to connect to these emails when we want to read them.

Configuration File Settings for Postfix Email Server

The main.cf file is the primary configuration file used by Postfix.

  1. If you are still logged in to MySQL, return to your main Linux shell by typing exit and hitting enter.
  2. Make a copy of the default Postfix configuration file in case you need to revert to the default configuration:
# cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
  1. Edit the /etc/postfix/main.cf file, replacing any occurrences of example.com with your domain name and verifying that the paths to the SSL certificate and private key are correct.
  • Here is an example file that can be used:
/etc/postfix/main.cf
# See /usr/share/postfix/main.cf.dist for a commented, more complete version

# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2

# TLS parameters
smtpd_tls_cert_file=/etc/letsencrypt/live/example.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/example.com/privkey.pem
smtpd_use_tls=yes
smtpd_tls_auth_only = yes
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtpd_sasl_security_options = noanonymous, noplaintext
smtpd_sasl_tls_security_options = noanonymous

# Authentication
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

# Restrictions
smtpd_helo_restrictions ==
 permit_mynetworks,
 permit_sasl_authenticated,
 reject_invalid_helo_hostname,
 reject_non_fqdn_helo_hostname
smtpd_recipient_restrictions ==
 permit_mynetworks,
 permit_sasl_authenticated,
 reject_non_fqdn_recipient,
 reject_unknown_recipient_domain,
 reject_unlisted_recipient,
 reject_unauth_destination
smtpd_sender_restrictions ==
 permit_mynetworks,
 permit_sasl_authenticated,
 reject_non_fqdn_sender,
 reject_unknown_sender_domain
smtpd_relay_restrictions ==
 permit_mynetworks,
 permit_sasl_authenticated,
 defer_unauth_destination

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = example.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydomain = example.com
myorigin = $mydomain
mydestination = localhost
relayhost ==
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all

# Handing off local delivery to Dovecot's LMTP, and telling it where to store mail
virtual_transport = lmtp:unix:private/dovecot-lmtp

# Virtual domains, users, and aliases
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf,
 mysql:/etc/postfix/mysql-virtual-email2email.cf

# Even more Restrictions and MTA params
disable_vrfy_command = yes
strict_rfc821_envelopes = yes
#smtpd_etrn_restrictions = reject
#smtpd_reject_unlisted_sender = yes
#smtpd_reject_unlisted_recipient = yes
smtpd_delay_reject = yes
smtpd_helo_required = yes
smtp_always_send_ehlo = yes
#smtpd_hard_error_limit = 1
smtpd_timeout = 30s
smtp_helo_timeout = 15s
smtp_rcpt_timeout = 15s
smtpd_recipient_limit = 40
minimal_backoff_time = 180s
maximal_backoff_time = 3h

# Reply Rejection Codes
invalid_hostname_reject_code = 550
non_fqdn_reject_code = 550
unknown_address_reject_code = 550
unknown_client_reject_code = 550
unknown_hostname_reject_code = 550
unverified_recipient_reject_code = 550
unverified_sender_reject_code = 550

The main.cf file declares the location of virtual_mailbox_domains, virtual_mailbox_maps, and virtual_alias_maps files.

  • These files contain the connection information for the MySQL lookup tables created in the MySQL section of this guide.
  • Postfix will use this data to identify all domains, corresponding mailboxes, and valid users.
Create the file for virtual_mailbox_domains.

Replace the value for password with your database user’s password.

  • If you used a different name for your database user and dbname replace those with your own values:
/etc/postfix/mysql-virtual-mailbox-domains.cf
user = mailuser
password = mailuserpass
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'
Create the /etc/postfix/mysql-virtual-mailbox-maps.cf file

enter the following values.

  • Use the database user’s password and make any other changes as needed:
/etc/postfix/mysql-virtual-mailbox-maps.cf
user = mailuser
password = mailuserpass
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'
Create the /etc/postfix/mysql-virtual-alias-maps.cf file

enter the following values.

  • Use the database user’s password and make any other changes as needed:
/etc/postfix/mysql-virtual-alias-maps.cf
user = mailuser
password = mailuserpass
hosts = 127.0.0.1
dbname = mailserver
query = SELECT destination FROM virtual_aliases WHERE source='%s'


Create the /etc/postfix/mysql-virtual-email2email.cf file

enter the following values.

  • Use the database user’s password and make any other changes as needed:
/etc/postfix/mysql-virtual-email2email.cf
user = mailuser
password = mailuserpass
hosts = 127.0.0.1
dbname = mailserver
query = SELECT email FROM virtual_users WHERE email='%s'
Restart Postfix
# systemctl restart postfix

Testing Postfix

We can test the Postfix configuration by using the postmap command, which can query Postfix’s lookup tables.# Enter the following command to ensure that Postfix can query the virtual_domains table.

  • Replace example.com with the first name value.
  • The command should return 1 if it is successful:
# postmap -q example.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
  1. Test Postfix to verify that it can retrieve the first email address from the MySQL table virtual_users.
  • Replace email1@example.com with the first email address added to the table.
  • You should receive 1 as the output:
# postmap -q email1@example.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
  1. Test Postfix to verify that it can query the virtual_aliases table.
  • Replace alias@example.com with the first source value created in the table.
  • The command should return the destination value for the row:
# postmap -q alias@example.com mysql:/etc/postfix/mysql-virtual-alias-maps.cf

Master Program Settings

  • Postfix’s master program starts and monitors all of Postfix’s processes.
  • The configuration file master.cf lists all programs and information on how they should be started.

Make a copy of the /etc/postfix/master.cf file:

# cp /etc/postfix/master.cf /etc/postfix/master.cf.orig
Edit /etc/postfix/master.cf to contain the values in the excerpt below.

The rest of the file can remain unchanged

#  File: /etc/postfix/master.cf
#
# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master" or
# on-line: http://www.postfix.org/master.5.html).
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)    (never) (100)
# ==========================================================================
smtp      inet  n       -       n       -       -       smtpd
#smtp      inet  n       -       -       -       1       postscreen
#smtpd     pass  -       -       -       -       -       smtpd
#dnsblog   unix  -       -       -       -       0       dnsblog
#tlsproxy  unix  -       -       -       -       0       tlsproxy
submission inet n       -       y      -       -       smtpd
 -o syslog_name=postfix/submission
 -o smtpd_tls_security_level=encrypt
 -o smtpd_sasl_auth_enable=yes
 -o smtpd_sasl_type=dovecot
 -o smtpd_sasl_path=private/auth
 -o smtpd_reject_unlisted_recipient=no
 -o smtpd_client_restrictions=permit_sasl_authenticated,reject
 -o milter_macro_daemon_name=ORIGINATING
smtps     inet  n       -       -       -       -       smtpd
 -o syslog_name=postfix/smtps
 -o smtpd_tls_wrappermode=yes
 -o smtpd_sasl_auth_enable=yes
 -o smtpd_sasl_type=dovecot
 -o smtpd_sasl_path=private/auth
 -o smtpd_client_restrictions=permit_sasl_authenticated,reject
 -o milter_macro_daemon_name=ORIGINATING
[...]
Change the permissions

of the /etc/postfix directory to restrict permissions to allow only its owner and the corresponding group

# chmod -R o-rwx /etc/postfix
Restart Postfix
# systemctl restart postfix