Configure sendmail with Gmail on Ubuntu


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

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

Requirements

Before starting, there are few requirements that need to configure sendmail with Gmail on Ubuntu −

  • A Gmail account

  • A 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 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 unique password that you are going to use for 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 top right corner and select "Google Account".

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

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

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

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

  • Note down 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 sendmail configuration file by running the following command −

sudo nano /etc/mail/sendmail.mc
  • Add following lines to the end of 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 file.

  • Regenerate the sendmail configuration by running following command −

sudo make -C /etc/mail
  • Restart the sendmail service by running 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 authentication file −

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

sudo nano /etc/mail/auth/client-info
  • Add 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 app password that you generated in Step 1.

  • Save and close the file.

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

sudo chmod 600 /etc/mail/auth/client-info
  • Regenerate the authentication database by running 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 following command to create a new 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 email to and verify that you have received email.

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

Advantages of Configuring Sendmail with Gmail

There are several advantages to configuring sendmail with Gmail on Ubuntu. Some of these include −

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

  • Improved Security − By using an app password instead of your regular Gmail password, you can improve 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 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 of 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, you need to create an app password for your Gmail account and add it to sendmail configuration file.

  • Authentication Failed − If you receive an error message that says "Authentication Failed", it means that app password that you used is incorrect. Double-check that you have entered 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 Gmail server. To fix this, you need to install the ca-certificates package by running 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 following command −

telnet smtp.gmail.com 587

If 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 straight forward process that can done in a few simple steps. By following steps outlined in this article, you can easily set up sendmail to send emails through your Gmail account. Once set up, you can use sendmail to send emails from Ubuntu server to any other email, making it convenient tool for email communication.

Updated on: 03-Mar-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements