I would like to send system mail from my ubuntu server via a remote SMTP server to my personal email. Here is how I have achieved that.
sudo apt update
sudo apt install postfix
create /etc/postfix/main.cf
-> sudo nano /etc/postfix/main.cf
# /etc/postfix/main.cf
# See /usr/share/postfix/main.cf.dist for a commented, more complete version
# Basic settings
myhostname = host.mydomain.com
#this for me is the reverse DNS for my server. Probarbly doesnt matter at all, as canonical_maps are used below.
myorigin = /etc/mailname
#myorigin = $myhostname
mydestination = $myhostname, localhost.$mydomain, localhost
relayhost = [smtp.mailgun.org]:587
# SMTP relay settings
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
# Email forwarding settings
#recipient_bcc_maps = hash:/etc/postfix/recipient_bcc
#virtual_alias_maps = hash:/etc/postfix/virtual
#generic maps
#smtp_generic_maps = hash:/etc/postfix/generic
recipient_canonical_maps = hash:/etc/postfix/recipient_canonical
sender_canonical_maps = hash:/etc/postfix/sender_canonical
# Additional settings
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydomain = mydomain.com
mynetworks = 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4
# Debugging settings
# Enable verbose logging if needed
# debug_peer_level = 2
# debug_peer_list = 127.0.0.1
# Uncomment the next line to generate "Received:" lines that can help with troubleshooting
# always_add_missing_headers = yes
# Uncomment the next line if you use SASL to authenticate the relay user
# smtp_sasl_tls_security_options = noanonymous
in order to send all system mail to my email I used recipient_canonical_maps and sender_canonical_maps, eg:
sudo nano /etc/postfix/sender_canonical
@myhostname mysenderemail@mydomain.com
#more specifically -> @host.mydomain.com mysenderemail@mydomain.com
then this needs to be hashed for the config file, as above:
sudo postmap /etc/postfix/sender_canonical
also:
sudo nano /etc/postfix/recipient_canonical
@myhostname myemail@isp.com
#this captures anything sent to the localhost eg root@myhostname etc
note smtp_generic_maps is commented, this failed as above, different settings for sender and recipient.
Also virtual_alias_maps commented, as this does not update the headers in the mail. Possibly acceptable however.
As for remote SMTP server, from ChatGPT:
Edit the main Postfix configuration file, main.cf
, usually located in /etc/postfix/
.
- Set the relay host: Add or modify the following lines to specify your external SMTP server:
relayhost = [smtp.external-server.com]:587
- Enable authentication (if required by the external SMTP server): Add the following lines:
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
- Create the
sasl_passwd
file: Add your SMTP credentials to the/etc/postfix/sasl_passwd
file:[smtp.external-server.com]:587 username:password
- Secure the
sasl_passwd
file: Run the following commands to secure thesasl_passwd
file and create the necessary hash:sudo chmod 600 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
sudo rm /etc/postfix/sasl_passwd
During testing etc reloading the config is required:
(this applies for recent ubuntu with systemd)
sudo systemctl restart postfix
can be tested with:
echo "This is a test email Night 8" | mail -s "Test Email Night 8" postmaster
note I had a default alias for postmaster to go to root, however neither matters, as both are captured by recipient_canonical_maps.
In summary Postfix is a labyrinth, and only with help from ChatGPT + local and remote logging was I able to navigate the config.