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.
# 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
Add the following lines to the /etc/mail/sendmail.mc configuration file right below the “MAILER DEFINITIONS” line:
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
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" firstname.lastname@example.org
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
Since Google’s implementation of OAuth 2.0, you may need to change the “Access for less secure apps” (which just means the client/app doesn’t use OAuth 2.0 – https://oauth.net/2/) for the gmail account you are trying to access. It’s found in the account settings on the Security tab, Account permissions (not available to accounts with 2-step verification enabled): https://support.google.com/accounts/answer/6010255?hl=en
Configuring PHP To Use Sendmail
Add the following lines to the /etc/php/version/fpm/php.ini file.
sendmail_path= /usr/sbin/sendmail -t -i
Restart PHP-FPM using your installed version of PHP (7.3 is shown):
systemclt restart php7.3-fpm