
Improve Server Email Deliverability: Configure Sendmail with Gmail SMTP Relay on Ubuntu/Debian
Ensuring that system-generated emails and notifications from your server actually reach their destination can be a significant challenge. By default, emails sent directly from a server’s IP address are often flagged as spam or rejected entirely by major email providers. A robust and reliable solution is to configure your server’s Mail Transfer Agent (MTA), like Sendmail, to relay messages through a trusted service such as Gmail.
This guide provides a comprehensive walkthrough for configuring Sendmail to use Gmail’s SMTP server on Ubuntu and Debian systems. By doing this, you leverage Google’s high-reputation infrastructure, dramatically increasing the deliverability and reliability of your server’s emails.
Prerequisites
Before you begin, ensure you have the following:
- A server running a recent version of Ubuntu or Debian.
- Root or
sudo
access to the server. - A Google account (Gmail).
- A Google App Password. For security, you should not use your main Google account password. An App Password is a 16-digit passcode that gives a non-Google app or device permission to access your Google Account. This is a critical security step that is required if you have 2-Factor Authentication (2FA) enabled, which is highly recommended.
Step 1: Install Sendmail and Authentication Packages
First, update your package list and install the necessary software. We need sendmail
itself, the sendmail-cf
package for configuration, and sasl2-bin
for handling authentication.
Open your terminal and run the following commands:
sudo apt-get update
sudo apt-get install sendmail sendmail-cf sasl2-bin
Step 2: Create and Secure the Gmail Authentication File
Next, you need to create a file that will securely store your Gmail credentials. Sendmail will use this file to authenticate with Google’s SMTP server.
Create a directory for your authentication information if it doesn’t already exist.
sudo mkdir -p /etc/mail/authinfo
Create a new file inside this directory called
gmail-auth
.sudo nano /etc/mail/authinfo/gmail-auth
Add the following line to the file, replacing
[email protected]
with your full Gmail address andyour-16-digit-app-password
with the App Password you generated.AuthInfo: "U:[email protected]" "I:[email protected]" "P:your-16-digit-app-password"
Save the file and exit the editor (Ctrl+X, then Y, then Enter in nano).
Step 3: Generate the Authentication Database
Sendmail doesn’t read the plain text file directly. You must convert it into a hashed database format (.db
) for Sendmail to use.
Use the
makemap
utility to create the database file.sudo makemap hash /etc/mail/authinfo/gmail-auth < /etc/mail/authinfo/gmail-auth
Secure the credential files by setting strict permissions so that only the root user can read or write to them. This prevents unauthorized users on the system from accessing your email credentials.
sudo chmod 600 /etc/mail/authinfo/gmail-auth*
Step 4: Configure the Sendmail Macro File (sendmail.mc
)
The main configuration for Sendmail is handled in the sendmail.mc
file. Here, we will instruct Sendmail to use Gmail as its “smart host” and provide the necessary authentication and security settings.
Open the
sendmail.mc
file with a text editor.sudo nano /etc/mail/sendmail.mc
Scroll to the bottom of the file and add the following block of code. This defines Gmail’s SMTP server as the relay, specifies the port, and points Sendmail to the authentication file you created.
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_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl FEATURE(`authinfo', `hash -o /etc/mail/authinfo/gmail-auth.db')dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
Save and close the file.
Step 5: Compile the Configuration and Restart Sendmail
After modifying the .mc
file, you must compile it into the .cf
format that Sendmail actually uses.
Run the
m4
macro processor to build the new configuration file.sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
Finally, restart the Sendmail service to apply all the new changes.
sudo systemctl restart sendmail
Step 6: Test Your Configuration
Your server should now be configured to send email through Gmail. You can test this by sending an email from the command line.
Run the following command, replacing [email protected]
with an email address you can access:
echo "Subject: Sendmail Gmail Relay Test\n\nThis is a test email sent from my Ubuntu server using Sendmail and Gmail." | sendmail -v [email protected]
The -v
(verbose) flag is useful here. It will show you the SMTP connection details in your terminal, allowing you to see if the connection to smtp.gmail.com
is successful and if the email is accepted for delivery. Check the recipient’s inbox (and spam folder, just in case) to confirm the email arrived.
Final Thoughts
By successfully relaying email through Gmail’s SMTP servers, you have created a far more professional and reliable notification system for your server. This setup bypasses common deliverability issues associated with dynamic or low-reputation IP addresses, ensuring that critical alerts, user notifications, and system reports consistently reach their intended recipients.
Source: https://kifarunix.com/configure-sendmail-to-use-gmail-relay-on-ubuntu-18-04-debian-10-9/