Sendmail and SMTP Setup on Ubuntu 24.04 with Easy Steps

This guide intends to teach you Sendmail and SMTP Setup on Ubuntu 24.04. Sendmail is an email server that you can use to send emails using SMTP (Simple Mail Transfer Protocol). So we decided to show you how you can install and configure Sendmail with SMTP on Ubuntu 24.04 on this guide from the Orcacore website.

Comprehensive Guide For Sendmail and SMTP Setup on Ubuntu 24.04

To set up Sendmail and SMTP on Ubuntu 24.04, you must access your Ubuntu server as a non-root user with sudo privileges. If you want to create a Sudo user, you can check this guide on creating a Sudo user for Ubuntu 24.04.

Sendmail and SMTP Setup on Ubuntu 24.04

1. Installing Sendmail on Ubuntu 24.04

First, you must run the system update and upgrade with the following command:

sudo apt update && sudo apt upgrade -y

Sendmail packages are available in the default Ubuntu 24.04 repository. You can easily use the following command to install sendmail:

sudo apt install sendmail -y

2. Add Hostname to /etc/hosts File on Ubuntu 24.04

In this step, you need to add your hostname to the /etc/hosts file. First, check your current hostname with the command below:

sudo hostname

Then, open the /etc/hosts file with your desired file text editor like Vi Editor and Nano Editor:

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.

3. SMTP Authentication For Sendmail

At this point, you need to create a new directory inside your /etc/mail directory for SMTP configuration. To do this, run the command below:

sudo mkdir /etc/mail/authinfo

Then, use the following command to set the correct permissions for the file:

sudo chmod -R 700 /etc/mail/authinfo

Now switch to the /etc/mail/authinfo file and create a SMPTP Auth file with the following commands:

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

Add the following line to the file with your Email address and password as shown below:

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

Once you are done, save and close the file. Next, you must create a hash database map for the above-created SMTP authentication with the following command:

sudo makemap hash smtp-auth < smtp-auth

4. Sendmail with SMTP Host Configuration

At this point, navigate to the /etc/mail directory and open the sendmail.mc file with the following commands:

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

At the file look for the MAILER _DEFINITIONS line and add the following configuration for Sendmail with SMTP below the line:

Note: Replace SMTP host with your 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

Once you are done, save and close the file.

Now rebuild the Sendmail configuration using the following command on Ubuntu 24.04:

sudo make

Then, restart Sendmail to apply the changes:

sudo /etc/init.d/sendmail restart

5. Configure Sendmail with PHP

If you want to use Sendmail with PHP, you must add a Sendmail path in your php.ini file. Open your file with your desired text editor:

sudo vi /etc/php/8.3/apache2/php.ini

At the end of the file, add the following line:

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

When you are finished, save and close the file. Restart Apache or PHP FPM to apply the changes:

# sudo service apache2 restart
or
# sudo service php8.3-fpm restart

6. SMTP Configuration without Authentication (Optional)

If you want to send emails without authentication you can follow the steps below. First, switch to your /etc/mail directory and open the sendmail.mc file with the commands below:

# cd /etc/mail 
# 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

Save and close the file, when you are done. Finally, rebuild the configuration and restart Sendmail on Ubuntu 24.04 with the commands below:

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

That’s it, you are done.

Conclusion

At this point, you have learned Sendmail and SMTP Setup on Ubuntu 24.04. You can easily install Sendmail, configure SMTP with Authentication, and start using Sendmail. Also, you can configure Sendmail without Authentication. Hope you enjoy using Sendmail with SMTP. Please subscribe to us on Facebook, Twitter, and YouTube.

Also, you may like to read the following articles:

Ubuntu 24.04 Run Tesseract OCR

PHP ionCube Loader For Ubuntu 24.04

Install Wireshark Ubuntu 24.04

Find Free Space Ubuntu 24.04

Ubuntu Core 24 by Canonical

FAQs

How can I test if Sendmail is working correctly?

You can test your Sendmail configuration by sending a test mail with the following mail command:
echo "This is a test email." | mail -s "Test Email" recipient@example.com

How do I monitor Sendmail logs for issues?

You can view your logs with the command below:
sudo tail -f /var/log/mail.log

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!