If you want to use a Gmail account as a free SMTP server on your Debian based server, you will find this article useful. This guide is tested with Raspbian GNU/Linux 8. If you face any issue, feel free to use comments-section below.
Relaying Postfix mails via smtp.gmail.com:
First, let’s install all necessary packages:
sudo apt-get install postfix mailutils libsasl2-2 ca-certificates libsasl2-modules
If postfix wasn’t installed before, configuration wizard will ask you some questions. Just select your server as Internet Site and for FQDN use something like mail.example.com
Open postfix main.cf using your favorite editor:
Add the following lines to it and save
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
Next edit /etc/postfix/sasl_passwd:
Add Gmail’s SMTP server and your credentials in it.
Fix permission and update postfix config to use sasl_passwd file.
sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd
Next, validate certificates to avoid running into error.
cat /etc/ssl/certs/thawte_Primary_Root_CA.pem | sudo tee -a /etc/postfix/cacert.pem
Reload postfix for changes to take effect:
sudo /etc/init.d/postfix reload
Check if mails are sent via Gmail SMTP server
If you have configured everything correctly, following command will send a test mail from your Linux box using your gmail account to your email (email@example.com).
echo "Test Gmail SMTP" | mail -s "Test Gmail SMTP" firstname.lastname@example.org
If mail sent from above command is actually sent via Gmail’s server, you can log into Gmail account USERNAME@gmail.com with PASSWORD and check “Sent Mail” folder in that Gmail account. By default, Gmail always keeps a copy of mail being sent through its web-interface as well as SMTP server. Once configured, all emails from your server will be sent via Gmail.
Please note that Gmail’s SMTP server has a limit of 500 emails per day. So use wisely!
Error: “SASL authentication failed; server smtp.gmail.com”
You need to unlock the captcha by visiting this page https://www.google.com/accounts/DisplayUnlockCaptcha
You can run test again after unlocking captcha.