How to Configure Sendmail to Relay Emails using Gmail SMTP on Ubuntu 18.04/16.04 LTS

In this article, We are going to perform How to Configure Sendmail to Relay Emails using Gmail SMTP on Ubuntu 18.04/16.04 LTS System.

Introduction

Sendmail is open source Mail Transfer Agent (MTA) used to transfer emails among hosts using Simple Mail Transfer Protocol (SMTP). It is widely used to send email using command line.Here we are configuring to transfer email using gmail smtp.

Prerequisites

  • Ubuntu 18.04/16.04 LTS with Minimal Installation
  • SSH Access with sudo privileges
  • Open Firewall Port: 587

Step 1: Remove/uninstall/stop postfix

Postfix is the default Mail Transfer Agent (MTA) in ubuntu. First, we have to remove postfix from ubunut, enter below commands to uninstall postfix.

$ sudo apt-get remove postfix
$ sudo apt-get purge postfix

Step 2: Install Sendmail on Ubuntu 18.04/16.04 LTS

First switch to root user.

$ sudo su

update the system database before installing any packages.

# apt-get update

Enter below command to install sendmail and other mail utilities if not installed.

# apt-get install sendmail sendmail-bin mailutils

Step 3: Configure Sendmail to Relay Emails using Gmail SMTP

1. Create Authentication File

We have to create new directory to store gmail account details.

#  mkdir -m 700 /etc/mail/authinfo

In above directory create a file named gmail-smtp-authentication and input your gmail account here.

#  nano /etc/mail/authinfo/gmail-smtp-authentication
/etc/mail/authinfo/gmail-smtp-authentication
AuthInfo: "U:root" "I:[email protected]" "P:GMAIL_USER_PASSWORD"

Replace your USER and GMAIL_USER_PASSWORD with your Gmail email id and password respectively.

2. Create Sendmail-Gmail Authentication Database HASH Map

We have to create database map from gmail-smtp-authentication file using makemap utility as below command.

# makemap hash /etc/mail/authinfo/gmail-smtp-authentication < /etc/mail/authinfo/gmail-smtp-authentication

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

3. Configure Sendmail SMART HOST

SMART HOST is the mail relay which allows SMTP server to route email to among hosts rather than sending to recipient  server. Here we are using smtp.gmail.com on port 587 to relay emails.

Note the below line;

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

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

Next, Copy and Paste below lines in /etc/mail/sendmail.mc file above MAILER_DEFINITIONS as shown below,

# nano /etc/mail/sendmail.mc
/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-authentication.db')dnl

dnl #
dnl # Default Mailer setup
MAILER_DEFINITIONS
MAILER(`local')dnl
MAILER(`smtp')dnl

4. Regenerate Sendmail Configuration

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

# make -C /etc/mail

Output:

make: Entering directory '/etc/mail'
Updating databases ...
Reading configuration from /etc/mail/sendmail.conf.
Validating configuration.
Creating /etc/mail/databases...
Updating auth ...
sasl2-bin not installed, not configuring sendmail support.

To enable sendmail SASL2 support at a later date, invoke "/usr/share/sendmail/update_auth"

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'

5. Reload Sendmail Service

After rebuilding Sendmail configuration, reload Sendmail.

# /etc/init.d/sendmail reload

Step 3: Testing Sendmail Gmail Relay

To verify that the sendmail configuration. Send a test mail using sendmail using below command.
# echo "test mail for sendmail gmail relay" | mail -s "Sendmail Test" [email protected]
sendmail configuration ubuntu
Check your Gmail Inbox, if you received email then you have successfully installed and configured Sendmail on Ubuntu Server. If not, there’s something wrong, check error log at tail -f /var/log/mail.log and fix the problem.

Step 4: Start, Restart and Stop Sendmail Service

$ sudo /etc/init.d/sendmail start

$ sudo /etc/init.d/sendmail restart

$ sudo /etc/init.d/sendmail stop

Conclusion

In this article, We have uninstalled postfix, 1.Installed Sendmail 2.Configured sendmail 3.Created Authentication File 4.Created Sendmail-Gmail Authentication Database HASH Map 5.Configure Sendmail SMART HOST 6.Regenerated Sendmail Configuration 6.Tested Sendmail Gmail Relay.

Related Articles

How to Install Redis on Ubuntu 18.04/16.04 LTS

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

We promise not to spam you, and we don't usually send more than one email a week.

 

You have Successfully Subscribed!

0 Shares
Share via
Copy link