Relay server messages to an external account with Postfix

From Luniwiki
Jump to: navigation, search

Software

apt update
apt upgrade
apt install postfix sasl2-bin mailutils

Relay host by users

We define relay host by users. To be able to have several senders.

vi relayhost_map
acc1@example.com        [smtp.oamis.net]:587
account@example.net     [smtp.oamis.net]:587

Hash the file

postmap hash:/etc/postfix/relayhost_map

Check the hash file has been created

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

Mail server credentials

We need to define the credentials that will be used to establish the connection with the smtp server. Because, we want multiple senders, we need to provide first the specific users and finally the default user. Create a file called sasl_passwd in /etc/postfix that contains the credentials.

vi /etc/postfix/sasl_passwd
acc1@example.com acc1@example.com:6xxxxxxx0
account@example.net account@example.net:8xxxxxx4
[smtp.oamis.net]:587 account@example.com:8xxxxxx4

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
/^acc1(.+)/ acc1@example.com
/.+/ account@example.com

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/main.cf and add/modify the following lines to our main.cf

cat /etc/postfix/main.cf | 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_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
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 = empcldgit01.reddog.microsoft.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = servername.example.com, $myhostname, servername, localhost.localdomain, localhost
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
inet_protocols = ipv4
relayhost = [smtp.oamis.net]:587
smtp_sasl_auth_enable = yes
smtp_sender_dependent_authentication = yes
sender_dependent_relayhost_maps = hash:/etc/postfix/relayhost_map
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" myemail@example.com <<EOF
> Hello
>
> EOF

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
root:   account@example.com
acc1:   acc1@example.com

Compile new aliases file

newaliases

Reload postfix

systemctl restart postfix

References

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