Marvin Tan Network Engineer • Open Source Lover

Send Email Using Google SMTP

#smtp #gmail Sep 27, 2019
Table of Contents:

I’m making a simple project that requires to send an email notification. Since I only need to send a few emails, I decided to go with Google’s free STMP server.

For more info, you can check Gmail’s sending limits here.

I did my testing using msmtp as SMTP client. It is available mostly on every Linux distribution.

I will show below how I made it working. I’m using Arch Linux but it will be the same with other distros.

1. Create Gmail account for testing

We will create 2 test Gmail accounts for this testing.

  1. Sender Email: smtp.test.user.0001@gmail.com
  2. Recipient Email: smtp.test.user.0002@gmail.com

Sender Account: create Google account

2. Install msmtp

sudo pacman -S msmtp

[marvin@arch ~]$ sudo pacman -S msmtp
[sudo] password for marvin:
resolving dependencies...
looking for conflicting packages...

Packages (2) gsasl-1.8.0-9  msmtp-1.8.5-1

Total Download Size:   0.58 MiB
Total Installed Size:  1.31 MiB

:: Proceed with installation? [Y/n]
:: Retrieving packages...
 gsasl-1.8.0-9-x86_64                         268.3 KiB  2.62M/s 00:00 [########################################] 100%
 msmtp-1.8.5-1-x86_64                         322.5 KiB  2.86M/s 00:00 [########################################] 100%
(2/2) checking keys in keyring                                         [########################################] 100%
(2/2) checking package integrity                                       [########################################] 100%
(2/2) loading package files                                            [########################################] 100%
(2/2) checking for file conflicts                                      [########################################] 100%
(2/2) checking available disk space                                    [########################################] 100%
:: Processing package changes...
(1/2) installing gsasl                                                 [########################################] 100%
(2/2) installing msmtp                                                 [########################################] 100%
:: Running post-transaction hooks...
(1/2) Arming ConditionNeedsUpdate...
(2/2) Updating the info directory file...
[marvin@arch ~]$

3. Create msmtp config file

Create file ~/.msmtprc

account default
host smtp.gmail.com
port 587
from smtp.test.user.0001@gmail.com
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
auth on
user smtp.test.user.0001
password s3cr3tP@ssw0rd

4. Send test email

Quickly check if mail client is working:

echo "test email 1" | msmtp -a default smtp.test.user.0002@gmail.com

You will encounter error 535-5.7.8

[marvin@arch ~]$ echo "test email 1" | msmtp -a default smtp.test.user.0002@gmail.com
msmtp: authentication failed (method PLAIN)
msmtp: server message: 535-5.7.8 Username and Password not accepted. Learn more at
msmtp: server message: 535 5.7.8  https://support.google.com/mail/?p=BadCredentials v15sm352780pfn.27 - gsmtp
msmtp: could not send mail (account default from /home/marvin/.msmtprc)

This is because Gmail will not allow unsecured app access.

critical security alert

5. Turn on less secure app access

Go to Settings > Google Account > Security and turn on Less secure app access

security settings

less secure app access

6. Send test email again

[marvin@arch ~]$ echo "test email 1" | msmtp -a default marvineric.tan@gmail.com
[marvin@arch ~]$

Voilà! It is now working! Check recipient’s email:

email received

That’s it. It is very simple to use Gmail’s free SMTP service.

Show comments