A Postfix SMTP relay configuration can be useful in several scenarios. Sometimes, for example, I want to have some email notifications sent to my email address by my computer at home, for example to receive reports of some scheduled tasks.
The problem is that if you just install Postfix or other MTA with a default configuration, emails sent from your home computer may be flagged as spam or mailing may not work altogether due to some restrictions ISPs often have also to prevent spam.
One workaround is to configure e.g. Postfix to use an external SMTP service such as SendGrid to send the emails. Here I’ll show how to do this on Gentoo.
First thing you need to do is install Postfix. Edit /etc/portage/package.use and add:
(of course you may have to specify a different version) Then run:
sudo emerge -av mail-mta/postfix
I also suggest you install mailutils as this includes an utility you can use to test email sending:
sudo emerge -av net-mail/mailutils
Next, you need to edit /etc/postfix/sasl_passwd and add the following line which contains the address and port of the SMTP service and the credentials required for the authentication:
You need then to create a db from this file with the following command:
sudo postmap /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 postmap /etc/mail/aliases
Now edit /etc/postfix/main.cf and add the following:
relayhost = [smtp.sendgrid.net]:587
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_CAfile = /etc/ssl/certs/ca-certificates.crt
myhostname = <hostname>
mydomain = <hostname>
Please note that you need to set a FQDN hostname on your computer that is already validated with the SMTP service.
Finally, restart Postfix:
sudo systemctl restart postfix.service
You can test that mailing works with the mail utility:
echo blah | mail -s "test" <your email address>
To check the logs you can run:
journalctl -f -u postfix
That’s it. All emails sent from your computer will be now sent through the 3rd party SMTP service.