How to Configure Sendmail to Relay Through Gmail on Debian Linux 10 (Buster)

This is a guide is for installing, setting up, and configuring sendmail to relay messages through Gmail or a Google Apps account. Upon successful completion you’ll be able to send email from your bash scripts, hosted website, or from the command line using the mail command. You can also use this for sending notifications of failed backups, cron jobs, and others. Sendmail is one of many utilities that can be configured to relay through a Gmail account. Some other utilities are postfix, exim, etc., but for the purpose of this guide we will be setting up sendmail on a Debian (buster) Linux box.

To get started, a few packages will need to be installed first. For this tutorial, we will work from the command line. WARNING: This should not be attempted by anyone lacking the skill set necessary to navigate the system, switch to superuser (or sudo), perform basic sysadmin, and know how to operate their editor of choice. Personally, I’m old school and still like using vi, but vim, nano, & emacs will get the job done too.

Install Packages

# apt-get install sendmail mailutils sendmail-bin

Create Gmail authentication file

Create a new directory within /etc/mail/ and apply root only permissions.

# mkdir -m 700 /etc/mail/authinfo/
# cd /etc/mail/authinfo/

Move into the directory and use vi (or your preferred text editor) to create a new file. Let’s call it, gmail-auth. Within that file add the following line:

AuthInfo: "U:root" "I:YOUR GMAIL EMAIL ADDRESS" "P:YOUR PASSWORD"

Next we need to create a hash map for the file you’ve just created:

# makemap hash gmail-auth < gmail-auth

Configuring Sendmail

Add the following lines to the /etc/mail/sendmail.mc configuration file right below the “MAILER DEFINITIONS” line:

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-auth.db')dnl

screenshot of /etc/mail/sendmail.mc configuration file settings for relaying messages through Gmail or a Google Apps account.

Next we need to execute the following command to rebuild the configuration for sendmail:

# make -C /etc/mail

Then reload the sendmail service by issuing the following command:

service sendmail reload

That’s it. All done. Now you can use the following command to send an email from the command line:

$ echo "Testing sendmail gmail relay" | mail -s "Sendmail Relay" your@email.com

If there is any significant delay in getting a return command prompt after issuing the above command, then check /etc/hostname to be sure it is set to a FQDN. If it is set to a short name, then there will be delays in getting a return command prompt as well as delays with receiving the test email. Check the log file (/var/log/mail.log) for the following warning (or something very similar):

sendmail: “unable to qualify my own domain name ( ) — using short name

Leave a Reply

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