Scenario: you have your own domain and have setup an email address in your server using, say, roundcube. Now, you want to send emails using a reliable free third party SMTP relay.
What steps you need to follow to use Gmail SMTP?
- You have to have postfix installed in your server. This post will show you how to configure postfix to use Gmail SMTP relay. But you to remember that Gmail put limit of how many emails you can send using their relay. If your service needs to sends thousands of email per day, you may considering buying third party SMTP relay service.
- Configure the Gmail account.
Install postfix using the following commands. In you already have installed posfix, just ignore the commands below.
sudo apt-get update
sudo apt-get install libsasl2-modules
sudo apt-get install postfix
Once you have installed postfix, open the main.cf file
And add or modify the following parameters.
myhostname = fqdn.example.com
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
create a password file
next go to /etc/postfix and create a file named sasl_passwd.
add the following lines:
Remember to replace the firstname.lastname@example.org and password with your gmail account name and password.
Next, create a Postfix lookup table from the sasl_passwd file using the postmap command. Run the following command:
the above command will create a file named sasl_passwd.db in /etc/postfix directory.
Change the ownership of sasl_passwd and sasl_passwd.db file
chown -R postfix:postfix /etc/postfix/sasl_passwd
chown -R postfix:postfix /etc/postfix/sasl_passwd.db
restart or reload the postfix
service postfix restart
service postfix reload
At first you need to turn on the less secure apps option in your Gmail account, otherwise you may not be able to send emails using your Gmail SMTP relay.
Type the following URL in your browser and then log to your Gmail account to allow the less secure apps.
Next, type https://accounts.google.com/DisplayUnlockCaptcha the browser and click on continue.
Add your domain’s email add in Gmail in order to show your own email address as source instead of Gmail address
If you use the default Gmail setting, your email source will not be your own domain name. It will be Gmail account address. To change this settings, you need to logon to your Gmail account and click on settings. Next click on “account and imports” and then click on “add another email address”. Type your name and then type the email address that you setup in your own domain.
Click on Next step. In the SMTP server name field type your full qualified domain name and your domain’s email address and password. Click on “Add account”.
After that, you will see a confirmation code box. To get this code login to your domain’s email account and copy and paste the code in your Gmail’s confirmation box.
If you want this new address to be your default address instead of Gmail handle, you can set as default address from the Gmail settings. Go to settitngs>account and imports>Send mail as: your email address. Next, click on make default.
Now, you can try to send email from your roundcube to any address and the recipient will see your domain email address instead of your Gmail address.
To identify the authorized SMTP relay for your domain, you can add a SPF record in your DNS.
TXT @ “v=spf1 a include:_spf.google.com ~all”
Note: the gmail SMTP works without SPF record though, you can add it to combat against spoofed emails.
Test email from your server to any domain of your choice.
echo “Test mail ” | mail -s “Test Postfix Email” email@example.com
For troubleshooting have a look at this page: https://productforums.google.com/forum/#!topic/apps/UqLETHGmUic
And also have a look at the error logs
If you do not find enough details in your mail log, add the following lines in the /etc/postfix/main.cf file and then try to send mail again and have a look at the maillog.