Postfix can be used to send mails to an external SMTP relay which is helpful if you want to setup notification sending from your server. The following steps will outline email sending using the Linux command line directly which can also be used for monitoring scripts that can send email notifications.
- This is tested on Ubuntu 16.04 LTS Server
- Working internet connection (able to connect to relay host: smtp.mymail.com)
- Firewall connection to smtp server (port 25 on this tutorial)
- Working email address for authentication ([email protected] and password)
Install the packages needed for mail sending – postfix and heirloom-mailx.
# apt update # apt install postfix heirloom-mailx -y
**NOTE: When postfix asks for Postfix configuration choose:
“Satellite system: All mail is sent to another machine, called a ‘smarthost’, for delivery.”
Sending via command line
After package installation, you should now be able to send email to the relay host using the mailx command, but you should input the needed credentials for authentication on the relay host. If you fail to provide credentials, the relay host may reject your email.
Use the following syntax:
# echo "test mail BODY" | mailx -r "[email protected]" -s "Subject Test" -S smtp="smtp.mymail.com:25" -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="mypassword" -a /file/attach.txt [email protected]
# mailx -r "[email protected]" -s "Subject Test" -S smtp="smtp.mymail.net:25" -S smtp-auth=login -S smtp-auth-user="[email protected]" -S smtp-auth-password="mypassword" -a /file/attach.txt [email protected] < /file/path/body.txt
echo – this portion will be the body of the message which is piped to mailx command
mailx – command to send email
-r – the FROM address the will show on the email client
-s – the subject of the email
-S – set options for mail sending, see below
smtp=”smtp.mymail.com:25″ – the mail relay host
smtp-auth=login – enables smtp authentication
smtp-auth-user=”[email protected]” – valid email address used to login to the smtp server
smtp-auth-password=”mypassword” – valid password of the email address used to login
-a – the filepath you want to attach for this email (if any)
[email protected] – the recipient email address
/file/path/body.txt – alternatively, if you have a large message body saved on a text file, use this syntax insertion instead of using echo command
Setup postfix and password database
As you may have noticed from the previous section, the email and password used for the relay host authentication is directly typed on the command line. This is saved in the history and can be viewed by other users. This is a security issue.
On this section, we’ll configure postfix to use the relay host and to encrypt the username/password that you use for authentication.
- Open or create the /etc/postfix/sasl_passwd file,
# vim /etc/postfix/sasl_passwd
- Add your destination (SMTP relay host), port, username, and password in the following format
# [smtp.mymail.com]:25 [email protected]:mysecretpassword
- Create the hash db file for Postfix by running the postmap command:
# postmap /etc/postfix/sasl_passwd
- You should now see that /etc/postfix/sasl_passwd and the /etc/postfix/sasl_passwd.db hash file were created.
# ls -l /etc/postfix/sasl_passwd /etc/postfix/sasl_passwd.db
- You can now delete the plain text file /etc/postfix/sasl_passwd so that your username/password is not visible to others. The /etc/postfix/sasl_passwd.db file is the encrypted file to be read by postfix.
# rm /etc/postfix/sasl_passwd
- Edit the main postfix config file /etc/postfix/main.cf.
Update/Add the following lines:
# specify SMTP relay host relayhost = [smtp.mymail.com]:587 # enable SASL authentication smtp_sasl_auth_enable = yes # disallow methods that allow anonymous authentication. smtp_sasl_security_options = noanonymous # where to find sasl_passwd smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
- Restart postfix
# service postfix restart
- Test mail sending again, without the authentication options:
# echo "body of email" | mailx -r "[email protected]" -s "test subject notif" -a /file/attach.txt [email protected]
# mailx -r "[email protected]" -s "test subject" -a /file/attach.txt [email protected] < /path/body.txt
If your mail is not sent, you may view the logs on /var/log/mail.log
You may also view the mail queue using the command: mailq