Relay server messages to an external account with Postfix

From Luniwiki
Jump to: navigation, search


apt update
apt upgrade
apt install postfix sasl2-bin mailutils

Mail server credentials

We need to define the credentials that will be used to establish the connection with the smtp server.

Create a file called sasl_passwd in /etc/postfix that contains the credentials.

vi /etc/postfix/sasl_passwd 

Hash the file

postmap hash:/etc/postfix/sasl_passwd

Check the hash file has been created

file /etc/postfix/sasl_passwd.db
/etc/postfix/sasl_passwd.db: Berkeley DB (Hash, version 9, native byte-order)

Change header FROM

Using smtp we can only send mail (FROM field in email header) as the user we are connecting with.

vi /etc/postfix/sender_canonical

Hash the file

postmap hash:/etc/postfix/sender_canonical

Check the hash file has been created

file /etc/postfix/sender_canonical.db
/etc/postfix/sender_canonical.db: Berkeley DB (Hash, version 9, native byte-order)

Configure postfix to be a relay

Edit /etc/postfix/ and add/modify the following lines to our

cat /etc/postfix/ | grep -v "^#" | grep -v "^$"
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
compatibility_level = 2
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname =
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination =, $myhostname, servername, localhost.localdomain, localhost
mynetworks = [::ffff:]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
inet_protocols = ipv4
relayhost = []:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_security_level = may
sender_canonical_maps = regexp:/etc/postfix/sender_canonical
smtp_use_tls = yes

Restart Postfix

systemctl restart postfix
systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
  Loaded: loaded (/lib/systemd/system/postfix.service; enabled; vendor preset: enabled)
  Active: active (exited) since Tue 2018-09-11 11:57:29 EDT; 7s ago
 Process: 3686 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 3686 (code=exited, status=0/SUCCESS)
Sep 11 11:57:29 empcldgit01 systemd[1]: Starting Postfix Mail Transport Agent...
Sep 11 11:57:29 empcldgit01 systemd[1]: Started Postfix Mail Transport Agent.

Send a test message

mail -s "test message" <<EOF
> Hello

Check the log file

tail /var/log/mail.log

Add aliases for local accounts

List aliases in file /etc/aliases

cat /etc/aliases
# See man 5 aliases for format
postmaster:    root

Compile new aliases file


Reload postfix

systemctl restart postfix


Daniel Simao 19:15, 11 August 2019 (EDT)