Share your love
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.
Table of Contents
Step 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.
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 your-hostname
When you are done, save and close the file.
Step 3 – Set up SMTP with Authentication on Debian 12
At this point, 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:email-address" "P:password"
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',`[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
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',`smtp-host')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.