Sendmail and SMTP Setup on Ubuntu 24.04 with Easy Steps
This guide provides a comprehensive walkthrough on how to configure Sendmail and SMTP Setup on Ubuntu 24.04. Sendmail is a powerful Mail Transfer Agent (MTA) that can be used to send emails using the Simple Mail Transfer Protocol (SMTP). This article, brought to you by Orcacore, will demonstrate the installation and configuration of Sendmail with SMTP on Ubuntu 24.04.
Comprehensive Guide For Sendmail and SMTP Setup on Ubuntu 24.04
Before you begin with the Sendmail and SMTP Setup on Ubuntu 24.04, ensure you have access to your Ubuntu server as a non-root user with sudo privileges. If you haven’t already, you can refer to Orcacore’s guide on creating a Sudo user for Ubuntu 24.04.

1. Installing Sendmail on Ubuntu 24.04
Start by updating and upgrading your system packages using the following command:
sudo apt update && sudo apt upgrade -y
The Sendmail package is available in the default Ubuntu 24.04 repositories. Install it with the following command:
sudo apt install sendmail -y
2. Add Hostname to /etc/hosts File on Ubuntu 24.04
Next, add your hostname to the /etc/hosts
file. First, check your current hostname using:
sudo hostname
Then, open the /etc/hosts
file with your preferred text editor (like Vi or Nano):
sudo vi /etc/hosts
On the line starting with 127.0.0.1
, append your hostname to the end of the line, like so (this should all be on one line):
127.0.0.1 localhost your-hostname
Save and close the file after editing.
3. SMTP Authentication For Sendmail
Create a new directory inside /etc/mail
for SMTP configuration:
sudo mkdir /etc/mail/authinfo
Set the correct permissions for the directory:
sudo chmod -R 700 /etc/mail/authinfo
Navigate to the /etc/mail/authinfo
directory and create an SMTP authentication file named smtp-auth
:
# cd /etc/mail/authinfo
# sudo vi smtp-auth
Add the following line to the file, replacing <email-address>
and <password>
with your actual email address and password:
AuthInfo: "U:root" "I:<email-address>" "P:<password>"
Save and close the file. Now, create a hash database map for the SMTP authentication using:
sudo makemap hash smtp-auth < smtp-auth
4. Sendmail with SMTP Host Configuration
Navigate to the /etc/mail
directory and open the sendmail.mc
file:
# cd /etc/mail
# sudo vi sendmail.mc
Locate the MAILER_DEFINITIONS
line and add the following configuration below it. Remember to replace smtp-host
with your actual SMTP server 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
Save and close the file. Rebuild the Sendmail configuration:
sudo make
Restart Sendmail to apply the changes:
sudo /etc/init.d/sendmail restart
5. Configure Sendmail with PHP
To use Sendmail with PHP, add the Sendmail path to your php.ini
file. Open the file:
sudo vi /etc/php/8.3/apache2/php.ini
Add the following line at the end of the file:
sendmail_path= /usr/sbin/sendmail -t -i
Save and close the file. Restart Apache or PHP FPM:
# 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, follow these steps. Navigate to the /etc/mail
directory and open the sendmail.mc
file:
# cd /etc/mail
# sudo vi sendmail.mc
Add the following configuration to the end of the file, replacing 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. Rebuild the configuration and restart Sendmail:
# cd /etc/mail
# sudo make
# sudo /etc/init.d/sendmail restart
That’s it, you’re done with Sendmail and SMTP Setup on Ubuntu 24.04!
Conclusion
You’ve now successfully completed the Sendmail and SMTP Setup on Ubuntu 24.04. You can install Sendmail, configure SMTP with or without authentication, and start using Sendmail for sending emails.
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
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
Alternative Solutions for Sending Emails on Ubuntu 24.04
While Sendmail is a robust solution, it can be complex to configure. Here are two alternative approaches for sending emails from an Ubuntu 24.04 server:
1. Using Postfix
Postfix is another popular and highly regarded Mail Transfer Agent (MTA). It’s known for its security, speed, and ease of configuration compared to Sendmail. It’s often the preferred choice for modern systems.
Explanation:
Postfix acts as a drop-in replacement for Sendmail and integrates well with other mail-related tools. Its configuration is generally simpler, utilizing a more human-readable configuration file structure. For basic sending functionality, Postfix can be configured with minimal effort.
Installation and Configuration:
-
Install Postfix:
sudo apt update sudo apt install postfix
During installation, you’ll be prompted to select a configuration type. For a basic setup to send emails from your server, choose "Internet Site". Enter your system’s fully qualified domain name (FQDN) when prompted. If you don’t have a registered domain name, you can use your server’s hostname.
-
Configure Postfix:
The main configuration file is
/etc/postfix/main.cf
. You’ll likely need to adjust themyhostname
,mydomain
, andmyorigin
settings.myhostname
: This should be your server’s hostname.mydomain
: This should be your domain name (if you have one).myorigin
: This specifies the domain that will appear in the "From" field of outgoing emails. You can set it to your domain name or your hostname.
Open the file:
sudo vi /etc/postfix/main.cf
Modify the lines to reflect your desired configuration. For example:
myhostname = your-hostname mydomain = your-domain.com myorigin = $mydomain
-
Reload Postfix:
After making changes to
main.cf
, reload Postfix to apply them:sudo systemctl reload postfix
-
Test Sending Email:
You can use the
mail
command to test sending an email:echo "This is a test email from Postfix" | mail -s "Postfix Test" recipient@example.com
Check the recipient’s inbox to confirm the email was received.
2. Using an External SMTP Relay (e.g., SendGrid, Mailgun)
Instead of running a local MTA like Sendmail or Postfix, you can leverage an external SMTP relay service. These services handle email delivery, reputation management, and other complexities, making sending emails more reliable.
Explanation:
SMTP relay services provide a dedicated infrastructure for sending emails. They typically offer higher deliverability rates, detailed analytics, and features like email tracking and suppression lists. This is particularly useful for applications that send a large volume of emails or require high reliability.
Configuration with msmtp
(Example):
msmtp
is a lightweight SMTP client that can be used to relay emails through an external SMTP server.
-
Install
msmtp
:sudo apt update sudo apt install msmtp msmtp-mta
-
Configure
msmtp
:Create a configuration file at
~/.msmtprc
(or/etc/msmtprc
for system-wide configuration):vi ~/.msmtprc
Add the following configuration, replacing the placeholders with your SMTP server details:
account default host your.smtp.server.com port 587 auth on user your_username password your_password from your_email@example.com tls on tls_starttls on tls_trust_file /etc/ssl/certs/ca-certificates.crt
host
: The SMTP server address (e.g.,smtp.sendgrid.net
).port
: The SMTP port (typically 587 for TLS).auth
: Set toon
to enable authentication.user
: Your SMTP server username.password
: Your SMTP server password.from
: The email address to use as the sender.tls
,tls_starttls
,tls_trust_file
: Configure TLS encryption for secure communication.
Set permissions on the configuration file to restrict access:
chmod 600 ~/.msmtprc
-
Configure
sendmail
symlink (if necessary):Since
msmtp-mta
is installed, a symlink should be created. Verify the sendmail binary points to msmtp.ls -l /usr/sbin/sendmail
If it does not, create a symlink to msmtp:
sudo ln -sf /usr/bin/msmtp /usr/sbin/sendmail
-
Test Sending Email:
Use the
mail
command to test:echo "This is a test email from msmtp" | mail -s "msmtp Test" recipient@example.com
These alternative methods offer simpler configuration or improved reliability compared to Sendmail for sending emails from your Ubuntu 24.04 server. Consider your specific needs and choose the solution that best fits your requirements. The Sendmail and SMTP Setup on Ubuntu 24.04 can be replaced with easier solutions.