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.
Configure Sendmail to Use Gmail Relay on Ubuntu 18.04/Debian 10/9
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
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.
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 18.04/Debian 10/9.
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
I was searching to configure sendmail, using your article it is working now. Thanks for sharing step by step.
Hello Shivdas.
Glad this helped you.
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.
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.
Hello,
How do you add a tls_policy?
Thx
Did you check mail logs, /var/log/mail.log?
Also, did you enable less secure app on Gmail?