Linux

How to Install and Setup Sendmail on Ubuntu

How to install and configure Sendmail on Ubuntu for sending emails using a email server which routes or relays the mail delivery.

Sendmail is a opensource Mail Transfer Agent (MTA) which is used to route email using server or by using shell commands. You can also configure SMTP using Sendmail.

In this guide you are going to learn how to install and setup Sendmail on Ubuntu. Also you will setup SMTP and configure it with PHP.

Best Hosting for your Business

PlatformReviewsPricing
Cloudways★★★★★$10
Kinsta – Google Cloud★★★★★$30

Update Server Packages

SSH inside your server and update the packages to it’s latest version.

sudo apt update
sudo apt upgrade

Install Sendmail

Sendmail is available in the ubuntu repository, so you can directly install using apt install command.

sudo apt install sendmail

Configure Hostname

Edit the /etc/hosts file and add your hostname.

sudo nano /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 hostname

Configure SMTP

Create new directory inside /etc/mail for SMTP configurations.

sudo mkdir /etc/mail/authinfo

Setup correct permissions.

sudo chmod -R 700 /etc/mail/authinfo

Create a new file for your SMTP authentication inside the newly created directory.

cd /etc/mail/authinfo
sudo nano smtp-auth

Paste the following line and replace the email-address with your login email and password with your password.

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

Hit CRTL + X followed by Y and ENTER to save and exit the file.

Create a hash database map for the above created authentication.

sudo makemap hash smtp-auth < smtp-auth

Configure SMTP

Navigate to the sendmail configuration directory and edit the sendmail.mc file.

cd /etc/mail
sudo nano sendmail.mc

Add the below configurations right after the MAILER _DEFINITIONS line.

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

The configuration should look like the same as the below screenshot.

SMTP configuration

Now save the file and exit the editor.

Rebuild Sendmail Configuration

Once the configuration is done you need to rebuild the Sendmail configuration using the make command.

cd /etc/mail
make

Once the configuration is rebuilt you need to restart Sendmail

Restart Sendmail

Restart Sendmail using the following command.

sudo /etc/init.d/sendmail restart

Now you can send emails using SMTP.

Additional Configurations

Configuration with PHP

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

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

To the bottom of the file add the following.

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

Restart Apache or PHP-FPM for the changes to take effect.

sudo service apache2 restart

or

sudo service php8.0-fpm restart

Sendmail SMTP Configuration without Auth

Incase if you have whitelisted your server IP for SMTP and you can send emails without authentication you can follow the below method.

You don’t need to create the smtp-auth file that we created above.

You can directly edit the sendmail.mc file and make the following changes.

cd /etc/mail
sudo nano sendmail.mc

Add the below configurations to the last.

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

Save the file, rebuild configuration and restart Sendmail.

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

Conclusion

Now you have learned how to install and configure Sendmail to relay using SMTP on Ubuntu.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.