Postfix SMTPS relay

Basic settings and tools to get a postfix relay working over SMTPS

Installation

Create any required accounts on your mail server.

Install required packages

yum install postfix cyrus-sasl -y

Configure postfix in /etc/postfix/main.cf

inet_protocols = ipv4
relayhost = smtp.company.com
smtp_sasl_auth_enable = yes  
smtp_sasl_password_maps = hash:/etc/postfix/smtp_sasl_password
smtp_sasl_security_options = noanonymous
smtp_use_tls = yes

Write down authent info for smtp server in /etc/postfix/smtp_sasl_password

smtp.company.com user:password

Generate smtp_sasl_password.db

postmap /etc/postfix/smtp_sasl_password

(Re)start postfix

systemctl restart postfix

Tests

Simple sendmail for postfix test

echo <<EOMAIL > sendmail yourself@company.com
From: software@company.com
To: yourself@company.com
Subject: Test email

.
EOMAIL

Python script w/o postfix in the loop, just to check smtp server

#!/usr/bin/env python
import smtplib

to = 'yourself@company.com'
user = 'software@company.com'
password = 'password'

smtpserver = smtplib.SMTP("smtp.company.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(user, password)
header = 'To:' + to + '\n' + 'From: ' + user + '\n' + 'Subject:Python test email \n'
msg = header + '\n This is a test email\n\n'
print(msg)
smtpserver.sendmail(user, to, msg)
smtpserver.close()

Usefull stuff

Display mail queue

mailq

Flush mailq

postfix flush

Display maillog

tail -f /var/log/maillog

To send all email from a fixed sender, if unknown user for exemple

echo '/.*/ no_reply@company.com' >> /etc/postfix/sender_canonical
postmap /etc/postfix/sender_canonical
echo 'sender_canonical_maps = regexp:/etc/postfix/sender_canonical' >> /etc/postfix/main.cf
systemctl restart postfix