Configure sendmail with Gmail on Ubuntu

Sendmail is a popular mail transfer agent used to send emails from one computer to another. It is often installed by default on Ubuntu, making it a convenient option for sending emails from a server. If you use Gmail, you can configure sendmail to send emails through your Gmail account.

In this article, we will show you the process of configuring sendmail with Gmail on Ubuntu.

Requirements

Before starting, there are a few requirements needed to configure sendmail with Gmail on Ubuntu

  • A Gmail account

  • An Ubuntu server

  • Sendmail installed on your Ubuntu server

  • OpenSSL installed on your Ubuntu server

If you don't have OpenSSL installed, you can install it by running the following command

sudo apt-get install openssl

Step 1: Create a Gmail App Password

To configure sendmail with Gmail, you need to create an app password. This is a unique password that you will use to authenticate your email account with sendmail.

To create an app password, follow these steps

  • Sign in to your Gmail account.

  • Click on your profile picture in the top right corner and select "Google Account".

  • Click on "Security" in the left-hand menu.

  • Scroll down to the "Signing in to other sites" section and click on "App passwords".

  • Select "Mail" as the app and "Other (custom name)" as the device.

  • Enter a custom name for your device and click on "Generate".

  • Note down the 16-digit password that is generated.

Step 2: Configure Sendmail

Now that you have created an app password for your Gmail account, you can configure sendmail to use it to send emails. Follow these steps to configure sendmail

  • Open the sendmail configuration file by running the following command

sudo nano /etc/mail/sendmail.mc
  • Add the following lines to the end of the file

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 /etc/mail/auth/client-info')dnl
  • Save and close the file.

  • Create the auth directory if it doesn't exist

sudo mkdir -p /etc/mail/auth
  • Regenerate the sendmail configuration by running the following command

sudo make -C /etc/mail
  • Restart the sendmail service by running the following command

sudo service sendmail restart

Step 3: Create an Authentication File

Next, you need to create an authentication file that contains the app password that you generated in Step 1. Follow these steps to create the authentication file

  • Create a new file in the /etc/mail/auth directory by running the following command

sudo nano /etc/mail/auth/client-info
  • Add the following lines to the file

AuthInfo:smtp.gmail.com "U:user@gmail.com" "P:16-digit-app-password"

Replace "user@gmail.com" with your Gmail address and "16-digit-app-password" with the app password that you generated in Step 1.

  • Save and close the file.

  • Set permissions for the authentication file by running the following command

sudo chmod 600 /etc/mail/auth/client-info
  • Regenerate the authentication database by running the following command

sudo makemap hash /etc/mail/auth/client-info < /etc/mail/auth/client-info

Step 4: Test Sendmail

Finally, you can test if sendmail is configured correctly by sending an email. Follow these steps to send an email using sendmail

  • Open a terminal window on your Ubuntu server.

  • Type the following command to create and send a test email

echo "Subject: Test email" | sendmail -v recipient@email.com

Replace "recipient@email.com" with the email address that you want to send the email to.

  • Press Enter and then type in the body of the email.

  • Press Ctrl+D to send the email.

  • Check the email address that you sent the email to and verify that you have received the email.

If you receive the email, then sendmail is working correctly and configured to send emails using your Gmail account.

Advantages

There are several advantages to configuring sendmail with Gmail on Ubuntu

  • Reliable Delivery When you configure sendmail with Gmail, you can rely on the stability and reliability of Gmail's servers to ensure that your emails are delivered successfully.

  • Improved Security By using an app password instead of your regular Gmail password, you can improve the security of your Gmail account and prevent unauthorized access.

  • Convenience Configuring sendmail with Gmail is a convenient way to send emails from your Ubuntu server. You can send emails from the command line or automate email tasks using scripts.

  • Low Cost Using sendmail with Gmail is a low-cost solution for sending emails from your Ubuntu server. You don't need to set up your own mail server or pay for an external email service.

Common Issues and Troubleshooting

While configuring sendmail with Gmail is a relatively straightforward process, there are some common issues that you may encounter. Here are some common issues and their solutions

  • "Relaying Denied" If you receive an error message that says "Relaying Denied", it means that your Gmail account is not authorized to send emails through sendmail. To fix this, ensure you have created an app password for your Gmail account and added it to the sendmail configuration file correctly.

  • Authentication Failed If you receive an error message that says "Authentication Failed", it means that the app password you used is incorrect. Double-check that you have entered the correct app password and try again.

  • SSL Certificate Verification If you receive an error message that says "SSL Certificate Verification Failed", it means that sendmail is not able to verify the SSL certificate of the Gmail server. To fix this, install the ca-certificates package by running the following command

sudo apt-get install ca-certificates
  • Port 587 Blocked If you receive an error message that says "Connection Refused" or "Connection Timed Out", it may be because port 587 is blocked on your server. You can check if port 587 is open by running the following command

telnet smtp.gmail.com 587

If the connection is refused or timed out, you may need to check your firewall settings or contact your hosting provider.

Conclusion

Configuring sendmail with Gmail on Ubuntu is a straightforward process that can be completed in a few simple steps. By following the steps outlined in this article, you can easily set up sendmail to send emails through your Gmail account. Once configured, sendmail becomes a convenient tool for automated email communication from your Ubuntu server.

Updated on: 2026-03-17T09:01:38+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements