Skip to main content

Postfix & S-nail

Prepare

VariableExample1Example2
smtp-servermail.spacemail.comsmtpdm.aliyun.com
smtp-port465465
sender-domainrectitude.devno-reply.rectitude.dev
user@sender-domainserver@rectitude.devserver@no-reply.rectitude.dev
password********

Install Postfix

dnf install -y postfix cyrus-sasl cyrus-sasl-plain
systemctl enable --now postfix

NOTE: sasl is required for SMTP authentication.

Set Default MTA (Mail Transfer Agent)

Run alternatives --config mta, and select sendmail.postfix

[root@webserver ~]# alternatives --config mta

There is 1 program that provides 'mta'.

Selection Command
-----------------------------------------------
* 1 /usr/sbin/sendmail.sendmail
+ 2 /usr/sbin/sendmail.postfix

Enter to keep the current selection[+], or type selection number: 2

Configure Postfix

/etc/postfix/main.cf
# e.g. [smtpdm.aliyun.com]:465
relayhost = [<smtp-server>]:<smtp-port>
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_wrappermode = yes
smtp_tls_security_level = encrypt
# e.g. webserver1.rectitude.dev
myhostname = <server-hostname.domain.com>
# e.g. noreply.rectitude.dev
myorigin = <sender-domain>
sender_canonical_maps = hash:/etc/postfix/sender_canonical

Configure SMTP Authentication

/etc/postfix/sasl_passwd
# e.g. [smtpdm.aliyun.com]:465 server@noreply.rectitude.dev:password
[<smtp-server>]:<smtp-port> <user@sender-domain>:<password>

Configure Sender Canonical

/etc/postfix/sender_canonical
# e.g. root@noreply.rectitude.dev server@noreply.rectitude.dev
root@<sender-domain> <user@sender-domain>

Secure Files and Map Configurations

chmod 600 /etc/postfix/sasl_passwd
chmod 600 /etc/postfix/sender_canonical
postmap /etc/postfix/sasl_passwd
postmap /etc/postfix/sender_canonical
systemctl restart postfix

Install S-nail

dnf install s-nail

Configure S-nail

~/.mailrc
# e.g. set from="WebServer1 <server@noreply.rectitude.dev>"
set from="WebServer1 <<user@sender-domain>>"

Send a Test Email

echo "Test Email Body" | s-nail -s "Test Subject" admin@rectitude.dev

Useful Commands

# View mail logs
tail -f /var/log/maillog
# View emails in the queue
postqueue -p
# Retry immediately
postfix flush
# Delete email
postsuper -d <queue ID>
# Clear all
postsuper -d ALL