It’s pretty easy to configure postfix on a local Linux box to forward emails via an external mail server. This way you can just send via localhost in your programs or any system daemons and the rest is automatically handled for you.
Here’s how to forward via GMail using authentication and encryption on Fedora (23 at the time of writing). You should consider enabling two-factor authentication on your gmail account, and generate a password specifically for postfix.
sudo dnf install cyrus-sasl-plain postfix mailx
Basic postfix configuration:
#Only listen on IPv4, not IPv6. Omit if you want IPv6.
sudo postconf inet_protocols=ipv4
#Relay all mail through to TLS enabled gmail
sudo postconf relayhost=[smtp.gmail.com]:587
#Use TLS encryption for sending email through gmail
sudo postconf smtp_use_tls=yes
#Enable authentication for gmail
sudo postconf smtp_sasl_auth_enable=yes
#Use the credentials in this file
sudo postconf smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
#This file has the certificate to trust gmail encryption
sudo postconf smtp_tls_CAfile=/etc/ssl/certs/ca-bundle.crt
#Require authentication to send mail
sudo postconf smtp_sasl_security_options=noanonymous
sudo postconf smtp_sasl_tls_security_options=noanonymous
By default postfix listens on localhost, which is probably what you want. If you don’t for some reason, you could change the inet_interfaces parameter in the config file, but be warned that then anyone on your network (or potentially the Internet if it’s a public address) could send mail through your system. You may also want to consider using TLS on your postfix server.
By default, postfix sets myhostname to your fully-qualified domain name (check with hostname -f) but if you need to change this for some reason you can. For our instance it’s not really necessary because we’re forwarding email through a relay and not accepting locally.
Check that our configuration looks good:
sudo postconf -n
sudo postfix check
Create a password file using a text editor:
The content should be in this form (the brackets are required, just replace your firstname.lastname@example.org address and password):
Hash the password for postfix:
sudo postmap /etc/postfix/sasl_passwd
Tail the postfix log:
sudo journalctl -f -u postfix.service &
Start the service (you should see it start up in the log):
sudo systemctl start postfix
Send a test email, replace email@example.com with your real email address:
echo "This is a test." | mail -s "test message" firstname.lastname@example.org
You should see the email go through the journalctl log and be forwarded, something like:
Feb 29 04:32:51 hostname postfix/smtp: 87BE620221: to=, relay=smtp.gmail.com[188.8.131.52]:587, delay=1.9, delays=0.04/0.06/0.55/1.3, dsn=2.0.0, status=sent (250 2.0.0 OK 1456720371 m32sm102235580ksj.52 - gsmtp)