Configuring sendmail with GMail relay on Ubuntu 16.04

Previously I published post How I configured sendmail for PHP on Ubuntu Server 12.04 describing how to configure sendmail to use custom SMTP server. But after switching to Ubuntu 16.04 VPS my first idea was that I can use sendmail in its default configuration, but this idea was wrong, because while sendmail worked well with yandex.ru (some Russian mail server), for example, it did not work with GMail, because GMail did not accept the mail reporting ‘DSN: Service unavailable.‘┬áin /var/log/mail.log, see my post on Ubuntu Forum for more information. Now my second idea is that using GMail as sendmail relay will prevent GMail from rejecting my mail. Below I provided a short instruction on how to realize this successful idea.

Install the packages

apt-get install sendmail mailutils

mailutils allows to run mail command.

Create authentication file

cd /etc/mail
mkdir -m 700 authinfo
cd authinfo/
nano gmail

put the following content into it:

AuthInfo: "U:root" "I:your-mail@gmail.com" "P:your-password"

create hash map of the file:

makemap hash gmail < gmail

this creates gmail.db in the same directory.

Specify GMail server parameters

Add the following lines to sendmail.mc file right above MAILER_DEFINITIONS:

#GMail settings:
define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail.db')dnl

make and reload:

cd /etc/mail
make
/etc/init.d/sendmail reload

If the first attempt of sending email from the command line fails:

echo "DeveloperNote.com A software developer's blog" | mail -s "Interesting website" mail-address@gmail.com

and the following message comes to GMail inbox:

open link https://www.google.com/settings/security/lesssecureapps and allow ‘less secure apps’:

and now you are done!

The only disadvantage of this method is that GMail overrides the sender address.

Links to similar articles:

  1. Configuring SendMail for Gmail relay
  2. Configuring Gmail as a Sendmail email relay
  3. Setup a SmartHost in Linux Sendmail Using Google Gmail

Leave a Reply

Your email address will not be published. Required fields are marked *