Postfix email relay with AWS SES on Linode Ubuntu 20.04
(based off of these directions):
sudo apt install -y postfix sudo cp /usr/share/postfix/main.cf.debian /etc/postfix/main.cf sudo postconf -e "relayhost = [email-smtp.us-west-2.amazonaws.com]:2587" "smtp_sasl_auth_enable = yes" "smtp_sasl_security_options = noanonymous" "smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd" "smtp_use_tls = yes" "smtp_tls_security_level = encrypt" "smtp_tls_note_starttls_offer = yes" sudo sh -c 'echo [email-smtp.us-west-2.amazonaws.com]:2587 AWSUSER:AWSPASS > /etc/postfix/sasl_passwd' sudo postmap hash:/etc/postfix/sasl_passwd sudo chown root:root /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db sudo chmod 0600 /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db sudo postconf -e 'smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt' sudo cp /etc/resolv.conf /var/spool/postfix/etc/resolv.conf # getting hostname unresolved without this. A bug in 14.04 that remains? sudo sh -c 'echo inet_interfaces = loopback-only >> /etc/postfix/main.cf' # smtp accepted but only internally sudo postfix stop; sudo postfix start; sudo postfix reload
Force the From address in Postfix
from these directions execute as root:
echo "sender_canonical_classes = envelope_sender, header_sender" >> /etc/postfix/main.cf echo "sender_canonical_maps = regexp:/etc/postfix/sender_canonical_maps" >> /etc/postfix/main.cf echo "smtp_header_checks = regexp:/etc/postfix/header_check" >> /etc/postfix/main.cf echo "/.+/ NEWSENDER@ADDR.COM" > /etc/postfix/sender_canonical_maps echo "/From:.*/ REPLACE From: NEWSENDER@ADDR.COM" >/etc/postfix/header_check sudo postfix stop; sudo postfix start; sudo postfix reload
Alternative allowing multiple from addresses to map to multiple to addresses (better than above solution?)
from https://serverfault.com/questions/147921/forcing-the-from-address-when-postfix-relays-over-smtp (example shown):
/etc/postfix/main.cf:
smtp_generic_maps = hash:/etc/postfix/generic
/etc/postfix/generic:
user@localdomain.local account@isp.example.com
@localdomain.local wholedomain@isp.example.com
Then do:
sudo postmap /etc/postfix/generic
sudo /etc/init.d/postfix reload
Remap root@localhost to another to address
- add
root: <EMAILADDR>to/etc/aliases sudo newaliases
test
Test with sendmail:
sendmail -f sender@example.com recipient@example.com # -f allows you to specify from and is not required From: Sender Name <sender@example.com> Subject: Amazon SES Test This message was sent using Amazon SES. .
Tips
- postfix should be set up with no configuration. When you do this, there are instructions given at the end to copy some sort of sample configuration.
- to reconfigure:
sudo dpkg-reconfigure postfix - to reload the settings:
sudo postfix stop; sudo postfix start; sudo postfix reload - when testing
tail -f /var/log/mail.logis your friend - view queued mail:
postqueue -p - flush queued mail:
postqueue -f - purge all queued mail:
sudo postsuper -d ALL - testing smtp connection and credentials from cli
- port 465:
swaks --to recipient@example.com --from sender@yourdomain.com --server smtp-server --port 465 --tls-on-connect --auth-user 'your_username' --auth-password 'your_password' - port 587:
swaks --to recipient@example.com --from sender@yourdomain.com --server smtp-server --auth-user 'your_username' --auth-password 'your_password' -p 587 --tls - port 465 is legacy and 587 is the better choice for modern systems
Optional
- get current hostname:
hostname - set hostname with
hostnamectl set-hostname <HOSTNAME>