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

5 Responses to Configuring sendmail with GMail relay on Ubuntu 16.04

  1. Tjolk says:

    Working great on my localdev, thank you.

  2. phpcore says:

    work perfectly but sendmail is horribly slow ๐Ÿ™

  3. Todd says:

    Thank you for this. I have followed the instructions, but seem to still be having issues.

    Whenever I attempt send test email with the echo … mail -s command, I see an error such as the following in /var/log/mail.log:
    sm-msp-queue[10813]: x2ULoMnF005979: to=root, ctladdr=gouldt (1000/1000), delay=09:57:33, xdelay=00:00:00, mailer=relay, pri=7500250, relay=[127.0.0.1] [127.0.0.1], dsn=4.0.0, stat=Deferred: Connection refused by [127.0.0.1]

    Also whenever I restart sendmail via sudo systemctl retsart sendmail.service I get the following in /var/log/mail.err
    sm-mta[10805]: NOQUEUE: SYSERR(root): /etc/mail/sendmail.cf: line 18: mailer AILER(local)MAILER(smtp)###: `=’ expected
    Mar 31 01:47:52 jupiter sm-mta[10805]: NOQUEUE: SYSERR(root): No local mailer defined
    Mar 31 01:47:52 jupiter sm-mta[10805]: NOQUEUE: SYSERR(root): QueueDirectory (Q) option must be set

    Could you please post an entire sendmail.mc (and any other relevant files) for comparison please. I am stuck and am fding no luck after extensive google searches.

    1. Todd says:

      Please ignore my previous message, I have resolved the issue on my own. Thank you!

      1. Trevor says:

        You have to tell people what you did to fix it Todd.

Leave a Reply

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