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.
The file should define Gmail authentication information should contain the line below;
AuthInfo: "U:root" "I:[email protected]" "P:GMAIL_USER_PASSWORD"
- 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;
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;
authinfo',hash -o /etc/mail/authinfo/gmail-smtp-auth.db')dnl
Replace it with your Gmail Authentication sendmail database map you generated above.
... 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'
After rebuilding Sendmail configuration, reload Sendmail.
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.