How To Install Sendmail on Ubuntu 22.04

In this tutorial, we want to teach you How To Install and Configure Sendmail on Ubuntu 22.04. Also, you will learn to Set up SMTP and configure it with PHP on Ubuntu 22.04.

Sendmail is a server application that allows businesses to send emails using the Simple Mail Transfer Protocol (SMTP). It’s typically installed on an email server on a dedicated machine that accepts outgoing email messages and then sends these messages to the defined recipient. It queues messages if a recipient is not immediately available and offers authentication as a method to prevent spam.

Sendmail, however, only sends email and does not have Post Office Protocol (POP) or Internet Message Access Protocol (IMAP), allowing it to receive messages and store them in user inboxes. Therefore, Sendmail is typically installed with additional applications that enable user inbox configurations.

Steps To Install and Configure Sendmail on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Install Sendmail on Ubuntu 22.04

By default, Sendmail packages are available in the default Ubuntu repository. First, update your local package index with the command below:

sudo apt update

Then, use the command below to install Sendmail:

sudo apt install sendmail

Configure Hostname on Ubuntu 22.04

At this point, you need to edit the /etc/hosts file and add your hostname to the file.

Find your hostname by using the command below:

hostname

Then, open the file with your favorite text editor, here we use vi:

sudo vi /etc/hosts

On the line starting with 127.0.0.1, add the hostname to the end as it looks below. This should be on a single line.

127.0.0.1 localhost your-hostname

When you are done, save and close the file.

Configure SMTP With Auth on Ubuntu 22.04

At this point, you need to create a new directory inside /etc/mail directory for SMTP configuration.

To do this, run the command below:

sudo mkdir /etc/mail/authinfo

Then, set the correct permission for the file:

sudo chmod -R 700 /etc/mail/authinfo

Now create a new file for your SMTP authentication inside the newly created directory by using the command below:

# cd /etc/mail/authinfo
# sudo vi smtp-auth

Add the following line to the file. Just remember to replace the email address with your login email and password with your password.

AuthInfo: "U:root" "I:email-address" "P:password"

When you are done, save and close the file.

Now you need to create a hash database map for the above-created authentication. To do this, run the following command:

sudo makemap hash smtp-auth < smtp-auth

SMTP Configuration

At this point, switch to the Sendmail configuration directory and edit the sendmail.mc file.

To do this, run the following commands:

# cd /etc/mail
# sudo nano sendmail.mc

Add the below configurations right after the MAILER _DEFINITIONS line.

Remember to replace smtp-host with your SMTP hostname.

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

When you are done, save and close the file.

Rebuild Sendmail Configuration on Ubuntu 22.04

At this point, you must rebuild the Sendmail configuration using the following command:

# cd /etc/mail
# sudo make

Then, restart Sendmail on Ubuntu 22.04 to apply the changes:

sudo /etc/init.d/sendmail restart

Now you can start to send emails by using SMTP.

How To Use Sendmail with PHP

To use Sendmail with PHP you need to add Sendmail path in your php.ini file.

Open the php.ini file with your favorite text editor, here we use vi:

sudo vi /etc/php/version/fpm-or-apache2/php.ini

To the bottom of the file add the following line:

sendmail_path= /usr/sbin/sendmail -t -i

When you are done, save and close the file.

Restart Apache or PHP-FPM to apply the changes:

sudo service apache2 restart

or

sudo service php8.1-fpm restart

Configure SMTP without Auth on Ubuntu 22.04

If you have whitelisted your server IP for SMTP and want to send emails without authentication you can follow the steps below.

First, switch to your /etc/mail directory:

cd /etc/mail

Then, open the sendmail.mc file:

sudo vi sendmail.mc

Add the below configurations to the end of the file.

Note: Replace smtp-host with your SMTP hostname.

define(`SMART_HOST',`smtp-host')dnl
define(`RELAY_MAILER', `esmtp')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl

When you are done, save and close the file.

Finally, rebuild the configuration and restart Sendmail on Ubuntu 22.04:

# cd /etc/mail
# sudo make
# sudo /etc/init.d/sendmail restart

That’s it you are done.

Conclusion

At this point, you have learned to Install and Configure Sendmail on Ubuntu 22.04. Also, you will learn to Configure SMTP with and without Authentication on Ubuntu 22.04.

Hope you enjoy it.

You may be like these articles:

Install and Configure Postfix Mail Server on Debian 11

Install and Configure Postfix Mail Server on Centos 7

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!