Configure Sendmail to Use Gmail Relay on Ubuntu|Debian

|
Last Updated:
|
|

In this guide, we are going to learn how to configure Sendmail to use Gmail Relay on Ubuntu 18.04/Debian 10/9. Sendmail can be configured to relay mails via external mail servers including Gmail SMTP.

Configuring Sendmail to Use Gmail Relay on Ubuntu/Debian

Install Sendmail on Ubuntu 18.04/Debian 10/9

Before you can install Sendmail, ensure that you remove other existing MTAs such as Postfix.

apt --purge autoremove postfix

Next, install Sendmail and other mail utilities if not installed already.

apt install sendmail sendmail-bin mailutils libsasl2-modules openssl

Define Gmail Authentication Details

Next, you need to create a directory to store a file that defines how to connect to Gmail SMTP server for mail relay. Hence, create a directory as shown below;

mkdir -m 700 /etc/mail/authinfo

Next, create a file to store Gmail authentication details under the directory created above.

vim /etc/mail/authinfo/gmail-smtp-auth

The file should define Gmail authentication information should contain the line below;

AuthInfo: "U:root" "I:[email protected]" "P:GMAIL_USER_PASSWORD"

Where:

  • U (The user (authorization) identifier): defines the name of the user that sendmail will use to check allowable permissions.
  • I (The authentication Identifier): Name of the user allowed to setup a connection.
  • P: The clear text for the authentication user used to authorize the mail connections.

Replace USER ID and GMAIL_USER_PASSWORD with your Gmail account user ID and password respectively.

NOTE that the use of less secure app access has been deprecated. See how you can use App password as an alternative in the guide below;

Configure Postfix to Use Gmail App Passwords

Create Sendmail-Gmail Authentication Database Map

Next, you need to generate the Sendmail gmail authentication databas map from the above authentication information. You can use the makemap utility as shown below;

makemap hash /etc/mail/authinfo/gmail-smtp-auth < /etc/mail/authinfo/gmail-smtp-auth

This will create authentication database, /etc/mail/authinfo/gmail-smtp-auth.db.

Configure Sendmail Smart Host

A smart host/smarthost is used to specify an SMTP server that relays emails, in this case, smtp.gmail.com. This can be defined using SMART_HOST macro.

The definition should made in /etc/mail/sendmail.mc configuration file JUST BEFORE the MAILER definition. If you need to create a comment, begin a line with dnl.

Note the line;

FEATURE(authinfo',hash -o /etc/mail/authinfo/gmail-smtp-auth.db')dnl

Replace it with your Gmail Authentication sendmail database map you generated above.

vim /etc/mail/sendmail.mc

...
dnl #
dnl # Defining Gmail Smarthost for sendmail
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-smtp-auth.db')dnl
dnl #
dnl # Default Mailer setup
MAILER_DEFINITIONS
MAILER(`local')dnl
MAILER(`smtp')dnl

Regenerate Sendmail Configuration

Save the configuration file above and re-build Sendmail configuration.

make -C /etc/mail

...
Creating /etc/mail/relay-domains
# Optional file...
Updating Makefile ...
Reading configuration from /etc/mail/sendmail.conf.
Validating configuration.
Creating /etc/mail/Makefile...
Updating sendmail.cf ...
The following file(s) have changed:
  /etc/mail/relay-domains /etc/mail/sendmail.cf
** ** You should issue `/etc/init.d/sendmail reload` ** **
make: Leaving directory '/etc/mail'

Reload Sendmail

After rebuilding Sendmail configuration, reload Sendmail.

/etc/init.d/sendmail reload

Testing Sendmail Gmail Relay

To verify that the setup actually works, try to send a test mail using sendmail. For example;

echo "This is a test for sendmail gmail relay" | sendmail [email protected]

You should be able to receive a mail with a body, This is a test for sendmail gmail relay. Also check the mail logs.

That is all on how to configure Sendmail to use Gmail relay on Ubuntu/Debian.

Install Zimbra Mail Server on Fedora 30/29/CentOS 7

Encrypt Emails using Enigmail on Thunderbird

Configure Postfix to Use Gmail SMTP on Ubuntu 18.04

How to Install and Setup Roundcube Webmail on Debian 9

How to Install and Setup iRedMail Mail Server on Ubuntu 18.04 LTS

How to Install and Setup Thunderbird Mail Client on Ubuntu 18.04

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

7 thoughts on “Configure Sendmail to Use Gmail Relay on Ubuntu|Debian”

  1. Hello.
    I am on Ubuntu 18.04.
    Tried to do everything suggested by you but still get a message that authentication is required by the gmail server.
    I was wondering about the syntax of the gmail-smtp-auth file. What does the U:root stand for exactly? For I: i am using my gmail address and in P the gmail password.

    Reply
    • I gave up on sendmail and tried to make it work on Postfix. I actually got the same issue there (message 530 from the gmail smtp server). After hours of digging through the information on the internet i finally solved it by adding a tls_policy. This may work for sendmail too.

      Reply
  2. This solution do not work.

    STARTTLS=client, relay=smtp.gmail.com., version=TLSv1.3, verify=FAIL, cipher=TLS_AES_256_GCM_SHA384, bits=256/256

    384FO2C9023750: AUTH=client, available mechanisms=LOGIN PLAIN do not fulfill requirements

    AUTH=client, relay=smtp.gmail.com., temporary failure, connection abort

    STARTTLS: read error=generic SSL error (-1), errno=9, get_error=error:0A000126:SSL routines::unexpected eof while reading, retry=99, ssl_err=1

    Quick fix for this problem:
    apt install libsasl2-modules openssl

    Reply

Leave a Comment