Sendmail is a popular open-source mail transfer agent (MTA) widely used on Unix-like operating systems, including Ubuntu. In this article, we will guide you through the steps to install Sendmail on Ubuntu 22.04 and configure it to send emails using the Gmail SMTP server. This setup allows you to send emails from your localhost or VPS server using your Gmail account without requiring DKIM and DMARC authentication.
Ubuntu 22.04: Send Email Using Gmail SMTP and Sendmail
Step 1. Update System and Install Sendmail
Begin by updating your system’s package lists to ensure you’re installing the latest versions:
sudo apt-get update
Next, install the Sendmail mail transfer agent (MTA) along with essential utilities:
sudo apt-get install sendmail mailutils sendmail-cf sasl2-bin libsasl2-modules
In this command:
sendmail
: The core Sendmail software responsible for sending emails.mailutils
: A collection of tools that allow you to send and receive emails from the command line and through PHP scripts.sendmail-cf
: Provides configuration files and tools for customizing Sendmail.sasl2-bin
: Enables authentication mechanisms for secure email delivery.
Step 2. Configure Mail Server
Run the following command to initiate the Sendmail configuration process:
sudo sendmailconfig
You’ll be prompted with several configuration questions. In most cases, you can simply press Y
(for “yes”) and Enter to accept the default settings:
- “Configure sendmail with the existing /etc/mail/sendmail.conf?” (Y)
- “Configure sendmail with the existing /etc/mail/sendmail.mc?” (Y)
- “Reload the running sendmail now with the new configuration?” (Y)
Step 3. Set Up Gmail SMTP
1. Add SMTP Configuration
To add Google SMTP configuration, navigate to the mail folder, open the file “sendmail.mc“, and add the following configurations. These configurations should be placed before the “MAILER_DEFINITIONS“.
sudo nano /etc/mail/sendmail.mc
# SMTP For Linux Ubuntu or Debian (itsmeit) 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-auth.db')dnl define(`confDOMAIN_NAME', `mail.arriveddev.com')dnl
Note: Replace the value for confDOMAIN_NAME
with your actual domain name. If you are setting up email from Ubuntu 20.04/22.04 localhost, you can configure the email address as mail.domain.com
. Then add mail.domain.com to the /etc/hosts
file.
I am using Nginx, and the domain name on my localhost is arriveddev.com
, so I will configure the localhost email address as mail.arriveddev.com
.
sudo nano /etc/hosts
127.0.0.1 localhost mail.arriveddev.com 127.0.1.1 arriveddev mail.arriveddev.com
2. Configure Gmail SMTP Settings
In step 1, we specified the location to create a file named “gmail-auth.db” in the “/etc/mail/authinfo” directory. However, this directory and file have not been created yet. To proceed, open a terminal and run the following commands to create the necessary directory and file:
sudo -s mkdir -m 700 /etc/mail/authinfo touch /etc/mail/authinfo/gmail-auth.db chmod 600 /etc/mail/authinfo/gmail-auth.db
3. Create a Gmail App Password
To create an app and password on Google you can follow these steps:
- Sign in to your Google account
- Turn on 2-step verification under “ Security ”
- Type in the search box the word “ App password ” and click on it to open it.
- Select “Other (Custom name)” and give your app a name.
- Click “ Generate ” to generate the application.
- Copy and save the password.
4. Link the App to Your Mail Server
Now that you have a Gmail App Password, you need to link it to your Sendmail server.
Create an Authentication File:
Create a file named “gmail-auth” and add the content below and make sure you use the email and password of the created “app” to configure sendmail Ubuntu Gmail SMTP.
sudo nano /etc/mail/authinfo/gmail-auth
Add the following lines to the file, replacing placeholders with your actual Gmail address and 16-digit App Password:
AuthInfo:smtp.gmail.com "U:buivanloi.2010@gmail.com" "P:16-digit-app-password"
Create a Hash Map:
Run the following command to create a hash map from the gmail-auth
file:
sudo makemap hash /etc/mail/authinfo/gmail-auth.db < /etc/mail/authinfo/gmail-auth
This command generates a database file (gmail-auth.db) that Sendmail can use for secure authentication.
Restart Sendmail:
Apply the configuration changes by restarting the Sendmail service:
sudo systemctl restart sendmail
Step 4. Test Email Sending
To test the email sending functionality, you can try triggering email notifications from your website for actions such as placing an order, resetting a password, signing up for an account, or subscribing to a newsletter.
Alternatively, you can use the following command line to send a test email from the terminal:
echo \ "Content: Test gmail" | mail -s \ "Subject: Test sendemail" -a \ "From: buivanloi.2010@gmail.com" -a \ "To: To <buivanloi.2010@gmail.com>" buivanloi.2010@gmail.com
Replace your@email.com
with your actual email address.
These are the steps to install and configure the Sendmail mail server on Ubuntu 22.04/24.04 or other Linux distributions like Debian. This configuration allows you to utilize Gmail’s SMTP server to send emails directly from your server (localhost or VPS) without the need for DKIM or DMARC authentication for that server. Remember to keep sensitive information secure and adhere to email sending best practices, avoiding spam to ensure the best possible experience.
This was exceedingly helpful, thank you so much
How can I ensure this runs and monitors the array on startup?