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 (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" "" "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 file right above MAILER_DEFINITIONS:

#GMail settings:
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail.db')dnl

make and reload:

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

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

echo " A software developer's blog" | mail -s "Interesting website"

and the following message comes to GMail inbox:

open link 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=[] [], dsn=4.0.0, stat=Deferred: Connection refused by []

    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/ 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 (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 *