Install Sendmail on Ubuntu 22.04 | Best Mail Server

Posted on

Install Sendmail on Ubuntu 22.04 | Best Mail Server

This tutorial, brought to you by Orcacore, will guide you through the process of installing Install Sendmail on Ubuntu 22.04. Furthermore, you’ll learn how to set up SMTP and configure it with PHP on your Ubuntu 22.04 server. Install Sendmail on Ubuntu 22.04 is a popular choice, so let’s dive in!

Sendmail is a robust Mail Transfer Agent (MTA), a server application designed to facilitate the sending of emails using the Simple Mail Transfer Protocol (SMTP). Typically, it’s installed on a dedicated email server that accepts outgoing email messages and relays them to the intended recipients. Sendmail efficiently manages email delivery by queuing messages when recipients are temporarily unavailable and offering authentication mechanisms to combat spam.

It’s important to note that Sendmail focuses primarily on sending emails. It lacks built-in Post Office Protocol (POP) or Internet Message Access Protocol (IMAP) functionality, which are essential for receiving and storing messages in user inboxes. Consequently, Sendmail is often paired with other applications to provide complete email server capabilities, including user inbox management.

Steps To Install and Configure Sendmail on Ubuntu 22.04

Before proceeding, ensure you’re logged into your server as a non-root user with sudo privileges. If needed, consult our guide on Initial Server Setup with Ubuntu 22.04 for assistance.

[Image of Sendmail Configuration on Ubuntu 22.04]

1. Install Sendmail on Ubuntu 22.04

Sendmail packages are readily available in the default Ubuntu repositories. Begin by updating your local package index:

sudo apt update

Next, install Sendmail using the following command:

sudo apt install sendmail

2. Configure Hostname on Ubuntu 22.04

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

First, determine your hostname using:

hostname

Then, open the /etc/hosts file with a text editor (e.g., vi or nano):

sudo vi /etc/hosts

Add your hostname to the end of the line starting with 127.0.0.1. It should resemble the following (on a single line):

[Image of Hostname Configuration for Sendmail]

Save and close the file.

3. Configure SMTP With Auth on Ubuntu 22.04

Create a new directory within /etc/mail for SMTP configuration:

sudo mkdir /etc/mail/authinfo

Set the correct permissions for the directory:

sudo chmod -R 700 /etc/mail/authinfo

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

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

Add the following line to the file, replacing email-address and password with your actual credentials:

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

Save and close the file.

Now, generate a hash database map for the authentication information:

sudo makemap hash smtp-auth < smtp-auth

SMTP Configuration for Sendmail

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

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

Add the following configurations immediately after the MAILER_DEFINITIONS line, replacing smtp-host with your SMTP server’s 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.

4. Rebuild Sendmail Configuration on Ubuntu 22.04

Rebuild the Sendmail configuration:

# cd /etc/mail
# sudo make

Restart Sendmail to apply the changes:

sudo /etc/init.d/sendmail restart

You can now send emails using SMTP.

5. How To Use Sendmail with PHP?

To integrate Sendmail with PHP, specify the Sendmail path in your php.ini file. Open the php.ini file using a text editor:

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

Add the following line to the bottom 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.1-fpm restart

6. Configure SMTP Without Auth on Ubuntu 22.04

If your server IP is whitelisted for SMTP and you want to send emails without authentication, follow these steps. Navigate to the /etc/mail directory:

cd /etc/mail

Open the sendmail.mc file:

sudo vi sendmail.mc

Add the following configurations to the end of the file, replacing smtp-host with your SMTP server’s 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.

Finally, rebuild the configuration and restart Sendmail:

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

That’s it! You’re done.

Conclusion

This tutorial covered how to Install Sendmail on Ubuntu 22.04 and configure SMTP with and without authentication. Hopefully, you found this guide helpful for Install Sendmail on Ubuntu 22.04.

You may also like these articles:

Alternative Solutions for Sending Emails on Ubuntu 22.04

While Sendmail is a venerable MTA, other solutions offer different advantages and complexities. Here are two alternative approaches to sending emails from an Ubuntu 22.04 server:

1. Using Postfix:

Postfix is another popular and powerful MTA, often considered easier to configure than Sendmail. It’s known for its security, performance, and modular design. It provides a solid alternative to Sendmail for Install Sendmail on Ubuntu 22.04.

  • Installation:

    sudo apt update
    sudo apt install postfix

    During installation, you’ll be prompted to choose a configuration type. Select "Internet Site" if your server will be directly sending emails.

  • Configuration:

    Postfix configuration is primarily managed through the /etc/postfix/main.cf file. Key parameters to configure include:

    • myhostname: The fully qualified domain name (FQDN) of your server.
    • mydomain: The domain name of your server.
    • myorigin: The domain that appears in the "From" header of outgoing emails. Typically set to $mydomain.
    • inet_interfaces: The network interfaces Postfix listens on. Set to all for all interfaces.
    • mydestination: The domains for which Postfix will accept mail for local delivery. Typically includes $myhostname, localhost.$mydomain, localhost, and $mydomain.
  • Relaying through an external SMTP server (using authentication):
    To relay email through a third-party SMTP service (like Gmail, SendGrid, or Mailgun), configure the following:

    sudo nano /etc/postfix/main.cf

    Add/modify the following lines:

    relayhost = [smtp.example.com]:587  # Replace with your SMTP server and port
    smtp_sasl_auth_enable = yes
    smtp_sasl_security_options = mayencrypt
    smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
    smtp_tls_security_level = encrypt

    Create the /etc/postfix/sasl_passwd file:

    sudo nano /etc/postfix/sasl_passwd

    Add the credentials (replace with your actual username and password):

    smtp.example.com:587  username:password

    Secure the file:

    sudo chown postfix:postfix /etc/postfix/sasl_passwd
    sudo chmod 600 /etc/postfix/sasl_passwd

    Create the hash database:

    sudo postmap /etc/postfix/sasl_passwd

    Restart Postfix:

    sudo systemctl restart postfix
  • PHP Configuration (for using Postfix with PHP):

    The sendmail_path in php.ini should point to the Postfix sendmail command:

    sudo vi /etc/php/version/fpm-or-apache2/php.ini #Replace version/fpm-or-apache2 with the correct paths
    

    Add or modify:

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

    Restart Apache or PHP-FPM.

2. Using an SMTP Client Library (SwiftMailer/PHPMailer):

Instead of relying on a local MTA like Sendmail or Postfix, you can use an SMTP client library directly within your PHP application. This approach offers more control over email sending and simplifies configuration, especially when relaying through external SMTP services. This removes the need to Install Sendmail on Ubuntu 22.04 altogether.

  • Installation (using Composer):

    composer require phpmailer/phpmailer
  • Code Example (using PHPMailer):

    <?php
    use PHPMailerPHPMailerPHPMailer;
    use PHPMailerPHPMailerSMTP;
    use PHPMailerPHPMailerException;
    
    require 'vendor/autoload.php'; // Adjust path if necessary
    
    $mail = new PHPMailer(true);
    
    try {
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_OFF;                      //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through (replace with your SMTP server)
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->Username   = 'your_email@example.com';                     //SMTP username (replace with your username)
        $mail->Password   = 'your_password';                               //SMTP password (replace with your password)
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;            //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
        $mail->Port       = 587;                                    //TCP port to connect to (replace with your port)
    
        //Recipients
        $mail->setFrom('from@example.com', 'Mailer');    //Set who the message is to be sent from (replace with your email)
        $mail->addAddress('recipient@example.com', 'Joe User');     //Add a recipient (replace with recipient email)
        //$mail->addAddress('ellen@example.com');               //Name is optional
        //$mail->addReplyTo('info@example.com', 'Information');
        //$mail->addCC('cc@example.com');
        //$mail->addBCC('bcc@example.com');
    
        //Attachments
        //$mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name
    
        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }

    Replace the placeholder values with your actual SMTP server details, username, password, sender email, and recipient email.

These alternative solutions provide flexibility and control over email sending from your Ubuntu 22.04 server. Choosing the right approach depends on your specific requirements, technical expertise, and the complexity of your email infrastructure. While this article focuses on Install Sendmail on Ubuntu 22.04, remember there are other options to consider.

Leave a Reply

Your email address will not be published. Required fields are marked *