How to Install and Configure Sendmail to Use Any Email Provider as Your SMTP Relay

This article assumes you’ll be using Ubuntu Server 20.04 or 24.04.

Motivation

Sendmail is renowned for its robust reliability, which is why it’s WordOps’ preferred Mail Transfer Agent (MTA). However, its configuration, particularly during tasks like authentication, encryption and compilation, can seem quite complex, even to seasoned system administrators. But fear not! With a clear, step-by-step understanding, these processes can actually be quite manageable. Let’s dive in and explore how to master this setup!

Alternatives

If you want to replace Sendmail, perhaps for something easier, options like Postfix, SSMTP or Nullmailer are all great choices. However, if you just need to send emails through WordPress and ignore all the valuable information coming from your server, FluentSMTP is probably the best option. Note that if you still need a SMTP plugin on your site, for logging functionality, for example, we can add it despite being already configured on the server.

Install Sendmail

Sendmail is part of WordOps Stack and can be easily installed with:

wo stack install --sendmail
sudo apt install libsasl2-modules

You can also install it in Ubuntu with APT:

sudo apt update
sudo apt install sendmail
sudo apt install libsasl2-modules

Installing mailutils is not a requirement but it gives sendmail more compatibility on your system:

sudo apt install mailutils

Create the Authentication File

Create a new file for authentication information in /etc/mail/authinfo, and add the following line to the file, replacing YOUR_USERNAME and YOUR_API_KEY with your actual Transactional Email Provider credentials (on some providers, a single token can be used as both the username and password):

AuthInfo:smtp.youremailprovider.com "U:YOUR_USERNAME" "P:YOUR_API_KEY"

There’s no need to set the authentication type since Sendmail will always choose the most secure method first, which we will define later as STARTLS.

Configure Sendmail to Use your SMTP Provider

Open the Sendmail configuration file located at /etc/mail/sendmail.mc. First modify the following line to set the domain you want to use for outgoing emails, replacing yourdomain.com with your actual domain:

Ubuntu 20.04

MASQUERADE_AS(`yourdomain.com')dnl

Ubuntu 24.04

On newer versions, Sendmail has removed all masquerade configurations, so you need to add all required settings like the following:

MASQUERADE_AS(`yourdomain.com')dnl
FEATURE(`masquerade_envelope')dnl
FEATURE(`masquerade_entire_domain')dnl

Then (on any version), add the following lines to configure Sendmail to use your provider as the SMTP relay, just after MAILER_DEFINITIONS, replacing smtp.youremailprovider.com with your actual Transactional Email Provider SMTP URL:

define(`SMART_HOST',`[smtp.postmarkapp.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.db')dnl

Generate the Database File

After saving the authinfo file, generate the database file that Sendmail will use:

sudo makemap hash /etc/mail/authinfo < /etc/mail/authinfo

Rebuild Sendmail Configuration

Sendmail need to compile all macros that it needs to use, including the ones in the file that we edited earlier. The compilation process will fail if there are any syntax error, so check it multiple times then run:

sudo m4 /etc/mail/sendmail.mc | sudo tee /etc/mail/sendmail.cf

Restart Sendmail

Restart the Sendmail service to apply the changes:

sudo service sendmail reload

Test Sending an Email

You can test your Sendmail configuration by sending a test email, but some providers may require the subject and also the body of the email, so let’s first create a file called test_email.txt with the content below:

Subject: Terminal Email Send

Email Content line 1
Email Content line 2

Now send an email using our file as base:

sendmail re*******@*****le.com < test_email.txt

Replace re*******@*****le.com with your actual email address.

Was this page helpful?

Leave a Reply

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