Update Server Packages
SSH inside your server and update the packages to it’s latest version.
sudo apt update
sudo apt upgrade
Install Sendmail
Sendmail is available in the ubuntu repository, so you can directly install using apt install command.
sudo apt install sendmail
Configure Hostname
Edit the /etc/hosts file and add your hostname.
sudo nano /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 hostname
Configure SMTP
Create new directory inside /etc/mail for SMTP configurations.
sudo mkdir /etc/mail/authinfo
Setup correct permissions.
sudo chmod -R 700 /etc/mail/authinfo
Create a new file for your SMTP authentication inside the newly created directory.
cd /etc/mail/authinfo
sudo nano smtp-auth
Paste the following line and replace the email-address with your login email and password with your password.
AuthInfo: "U:root" "I:email-address" "P:password"
Hit CRTL + X followed by Y and ENTER to save and exit the file.
Create a hash database map for the above created authentication.
sudo makemap hash smtp-auth < smtp-auth
Configure SMTP
Navigate to the sendmail configuration directory and edit the sendmail.mc file.
cd /etc/mail
sudo nano sendmail.mc
Add the below configurations right after the MAILER _DEFINITIONS line.
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
The configuration should look like the same as the below screenshot.
SMTP configuration
Now save the file and exit the editor.
Rebuild Sendmail Configuration
Once the configuration is done you need to rebuild the Sendmail configuration using the make command.
cd /etc/mail
make
Once the configuration is rebuilt you need to restart Sendmail
Restart Sendmail
Restart Sendmail using the following command.
sudo /etc/init.d/sendmail restart
Now you can send emails using SMTP.
Additional Configurations
Configuration with PHP
To use Sendmail with PHP you need to ad sendmail path in your php.ini file.
sudo nano /etc/php/version/fpm-or-apache2/php.ini
To the bottom of the file add the following.
sendmail_path= /usr/sbin/sendmail -t -i
Restart Apache or PHP-FPM for the changes to take effect.
sudo service apache2 restart
or
sudo service php8.0-fpm restart
Sendmail SMTP Configuration without Auth
Incase if you have whitelisted your server IP for SMTP and you can send emails without authentication you can follow the below method.
You don’t need to create the smtp-auth file that we created above.
You can directly edit the sendmail.mc file and make the following changes.
cd /etc/mail
sudo nano sendmail.mc
Add the below configurations to the last.
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 the file, rebuild configuration and restart Sendmail.
cd /etc/mail
make
sudo /etc/init.d/sendmail restart
No comments:
Post a Comment