Install Sendmail and Set up SMTP on Debian 12 Bookworm – Best Steps

Posted on

Install Sendmail and Set up SMTP on Debian 12 Bookworm - Best Steps

Install Sendmail and Set up SMTP on Debian 12 Bookworm – Best Steps

In this guide, you will learn to Install Sendmail and Set up SMTP on Debian 12 Bookworm. Sendmail is a server application you can use to send emails using the Simple Mail Transfer Protocol (SMTP). Typically, it is installed on an email server on a dedicated machine that accepts outgoing email messages and then sends these messages to the defined recipient.

You can follow this instruction from the Orcacore team to Install Sendmail and Set up SMTP on Debian 12 Bookworm.

To Install Sendmail and Set up SMTP on Debian 12 Bookworm, you must have access to your server as a non-root user with sudo privileges. For this purpose, you can visit this guide on Initial Server Setup with Debian 12 Bookworm.

Install Sendmail and Set up SMTP on Debian 12 Bookworm
Install Sendmail and Set up SMTP on Debian 12 Bookworm

Step 1 – Install Sendmail on Debian 12

To Install Sendmail and Set up SMTP on Debian 12 Bookworm, you must run the system update with the following command:

sudo apt update

Sendmail packages are available in the default Debian 12 repository. So you can use the command below to install Sendmail:

sudo apt install sendmail

When your installation is completed, proceed to the next step to configure your hostname.

Step 2 – Configure /etc/hosts File on Debian 12

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 <mark>your-hostname</mark>

When you are done, save and close the file.

Step 3 – Set up SMTP with Authentication on Debian 12

To Install Sendmail and Set up SMTP on Debian 12 Bookworm, 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:<mark>email-address</mark>" "P:<mark>password</mark>"

When you are done, save and close the file.

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

sudo makemap hash smtp-auth < smtp-auth

Step 4 – Configure Sendmail SMPT Host on Debian 12

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 vi sendmail.mc

Add the below configurations right after the MAILER _DEFINITIONS line.

Remember to replace SMTP-host with your SMTP hostname.

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

At this point, you must rebuild the Sendmail configuration using the following command on Debian 12 Bookworm:

sudo make

Then, restart Sendmail on Debian 12 to apply the changes:

sudo /etc/init.d/sendmail restart

Now you can start to send emails by using SMTP.

Step 5 – How To Use Sendmail with PHP on Debian 12?

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 the vi editor:

sudo vi /etc/php/8.2/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

Step 7 – Configure SMTP without Authentication (Optional)

If you 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',`<mark>smtp-host</mark>')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 Debian 12:

# 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 Sendmail and Set up SMTP on Debian 12 Bookworm. By following key steps including installing the necessary packages, configuring authentication, and securing the setup with TLS, you can ensure reliable and secure email transmission. Sendmail’s flexibility and compatibility with SMTP make it a robust solution for managing email servers.

Hope you enjoy it. You may also interested in these articles:

Nagios Core Installation on Debian 12 Bookworm

Set up Node.js LTS on Debian 12 Bookworm

FAQs

What is Sendmail, and why use it?

Sendmail is a server application used to send emails via SMTP. It is typically installed on dedicated email servers.

How do you install Sendmail on Debian 12?

You can install it using the command: `sudo apt install sendmail`
You can easily follow the above guide steps to Install Sendmail and Set up SMTP on Debian 12 Bookworm.

How do you configure SMTP with authentication?

By creating an authentication file inside /etc/mail/authinfo, configuring sendmail.mc, and creating a hash database.

Alternative Solutions for Sending Emails on Debian 12

While Sendmail is a venerable and powerful MTA (Mail Transfer Agent), it can be complex to configure and maintain. Several alternative solutions offer simpler setup and often better performance, especially for applications that just need to send out emails and not necessarily run a full-fledged email server. Here are two alternative approaches to sending emails on Debian 12:

1. Using Postfix (Simpler MTA)

Postfix is another MTA that is often considered a more modern and easier-to-configure alternative to Sendmail. It is designed for security and performance and is widely used.

Explanation:

Postfix simplifies the configuration process while providing robust email delivery capabilities. It’s an excellent choice if you need a reliable MTA without the complexities associated with Sendmail’s configuration files.

Installation and Configuration:

  1. Install Postfix:

    sudo apt update
    sudo apt install postfix

    During the installation, you will be prompted to choose a configuration type. Select "Internet Site" if you want to send emails directly.

  2. Configure Postfix:

    The main configuration file for Postfix is /etc/postfix/main.cf. Open it with your favorite text editor:

    sudo nano /etc/postfix/main.cf
  3. Basic Configuration:

    Ensure the following parameters are set correctly:

    • myhostname: This should be set to your server’s hostname.
    • mydomain: This should be set to your domain name.
    • myorigin: Set this to $mydomain for emails to appear to originate from your domain.
    • inet_interfaces: Set this to all to listen on all interfaces.
    • mynetworks: Define the networks allowed to relay mail. For a single server, you can usually set this to 127.0.0.0/8.
    • relayhost: If you want to use an external SMTP server (e.g., Gmail, SendGrid), specify it here in the format [smtp.example.com]:587. You may need to configure authentication as well (see below).

    Example configuration snippet:

    myhostname = yourserver.example.com
    mydomain = example.com
    myorigin = $mydomain
    inet_interfaces = all
    mynetworks = 127.0.0.0/8
    relayhost = [smtp.example.com]:587 #If using external SMTP
  4. Using an External SMTP Server (with Authentication):

    If you are using an external SMTP server, you’ll need to configure authentication.

    • Install sasl2-bin:

      sudo apt install sasl2-bin
    • Create a password file /etc/postfix/sasl_passwd:

      sudo nano /etc/postfix/sasl_passwd

      Add your SMTP server credentials:

      [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
    • Edit /etc/postfix/main.cf to include the following:

      smtp_sasl_auth_enable = yes
      smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
      smtp_sasl_security_options = noanonymous
      smtp_tls_security_level = may
      header_size_limit = 4096000
      smtp_use_tls = yes
  5. Restart Postfix:

    sudo systemctl restart postfix

2. Using a Transactional Email Service (API-Based)

Transactional email services like SendGrid, Mailgun, or Amazon SES provide APIs to send emails. This approach offloads the complexities of email infrastructure and deliverability to specialized providers.

Explanation:

Using a transactional email service is beneficial because they handle email deliverability, spam filtering, and bounce management. This approach is ideal for applications that need to send emails reliably without managing an MTA.

Implementation:

  1. Choose a Service:

    Select a transactional email service and create an account (e.g., SendGrid, Mailgun, Amazon SES).

  2. Install a Client Library:

    Most programming languages have libraries to interact with these services. For example, if you’re using PHP, you can use the SendGrid PHP library.

    composer require sendgrid/sendgrid
  3. Code Example (PHP using SendGrid):

    <?php
    require 'vendor/autoload.php'; // If using Composer
    
    $apiKey = getenv('SENDGRID_API_KEY'); // Get API key from environment variable
    $email = new SendGridMailMail();
    $email->setFrom("test@example.com", "Example User");
    $email->setSubject("Sending with SendGrid is Fun");
    $email->addTo("recipient@example.com", "Recipient Name");
    $email->addContent(
        "text/plain", "and easy to do anywhere, even with PHP"
    );
    $email->addContent(
        "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
    );
    $sendgrid = new SendGrid($apiKey);
    try {
        $response = $sendgrid->send($email);
        print $response->getStatusCode() . "n";
        print_r($response->headers());
        print $response->body() . "n";
    } catch (Exception $e) {
        echo 'Caught exception: '. $e->getMessage() ."n";
    }
    ?>

    Explanation of the Code:

    • The code uses the SendGrid PHP library to construct and send an email.
    • SENDGRID_API_KEY is an environment variable storing your SendGrid API key. This keeps your key secure.
    • The code sets the sender, recipient, subject, and content (both plain text and HTML) of the email.
    • The $sendgrid->send($email) call sends the email, and the code handles any exceptions that may occur.

Choosing the Right Approach:

  • Sendmail: Best for complex email server setups where you need full control and flexibility. Requires significant configuration and maintenance expertise.
  • Postfix: A simpler and more modern MTA that’s easier to configure than Sendmail. Suitable for servers that need to send emails directly but don’t require advanced features.
  • Transactional Email Service: The easiest option for applications that need reliable email delivery without managing an MTA. Good for transactional emails like password resets, notifications, and confirmations. Comes with a cost based on email volume.

By understanding these alternatives, you can choose the solution that best fits your needs and technical expertise when setting up email sending on Debian 12 Bookworm.

Leave a Reply

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