• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

securitywing

How to setup Gmail SMTP relay to send email from your domain?

by wing

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?

  1. 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.
  2. 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

/etc/postfix/main.cf

And add or modify the following parameters.

myhostname = fqdn.example.com

relayhost =[smtp.gmail.com]:587

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.

nano sasl_passwd

add the  following lines:

[smtp.gmail.com]:587   yourid@gmail.com:password

Remember to replace the yourid@gmail.com 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:

postmap /etc/postfix/sasl/sasl_passwd

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

or

service postfix reload

Gmail Settings

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.

https://www.google.com/settings/security/lesssecureapps

turn on access for less secure apps in gmail

Next, type https://accounts.google.com/DisplayUnlockCaptcha the browser and click on continue.

allow access to gmail account

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.

add another email address in gmail for smtp relay

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”.

add smtp server name in gmail for postfix

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.

SPF Record

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”  yourname@anydomain.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

/var/log/maillog

/var/log/mail.err

/var/log/mail.log

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.

debug_peer_list=smtp.gmail.com

debug_peer_level=3

Related posts:

  1. Exchange Server 2003 Tutorial
  2. Windows DNS Server Configuration
  3. Types of Virtualization Technology-Advantages vs. Disadvantages
  4. How to Configure SNMP in Zabbix Server (Debian) to monitor Cisco Switches

Filed Under: Tutorial Tagged With: gmail relay, smtp

Primary Sidebar

Please help us sharing

Categories

  • AWS
  • Basics
  • Containers
  • Cryptocurrency
  • Cyber
  • Internet Security and Safety
  • IS Audit
  • IT Security Exams
  • Law & Human Rights
  • Network Security Tips
  • Off Track
  • Social Media Governance
  • Tech Comparisons
  • Tech Stack Suitability
  • Telecom
  • Tutorial

CISSP Sample Test

Take a CISSP Sample Test

CISA Sample Test

CISA IT governance Sample test

Please Follow Us

Contact us for Ads

Go to Contact Form

Search

Footer

Copyrights

Protected by Copyscape Duplicate Content Detection Software

Securitywing.com reserves the copyrights of all of its published articles.No contents of this site is permitted to be published to anywhere else in the Internet.If any contents are found in any other websites, securitywing reserves the rights to file a DMCA complaint. But you have the right to use the link of any relevant article of this site to point from your website if you consider that it might improve the quality of your article.

Tags

audit AWS backup basics browser check cisco cloud computer configuration cyber data database email gmail hsrp ids iis informaiton internet kubernetes linux load balancing malware microsoft network protection redundancy risk router security security tips server social media SSL switch test tools vpn vrrp web webserver website windows wordpress

Copyright © 2010-2025 ·All Rights Reserved · SecurityWing.com