Introduction

Sendmail is a common email transfer agent. It has been around since 1983 and is still widely used on many email servers.

Installation

sendmail-cf rpm must also be installed. the sendmail-cf package provides the makefiles2 and configuration files that allow configuration and recompilation of sendmail.mc and other Sendmail configuration files and databases.
We also install mailx, an email client that can be used as a text mode email client and as a command in a pipeline to send a data stream from its StdIn to the local mail transfer agent. this is a good tool for use to send emails in scripts. We can also use it from the command line to easily send test email.

# dnf -y install sendmail sendmail-cf mailx
Configuration

In order to receive email from any remote computer on your virtual network, you will need to comment out the following line in /etc/mail/sendmail.mc. this line forces Sendmail to listen for email only on the internal lo localhost interface. We want Sendmail to listen on the external interface, emnp0s8, as well.

DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl

Prepend “dnl” to the above line in order to “comment it out.” Sendmail will now listen for inbound emails on all network interfaces.

dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl

In the m4 language used in sendmail.mc, dnl means “delete through newline,” which further translates into something meaningful as “ignore the rest of this line.” It is an instruction for the specialized compiler used by Sendmail.

local Host Name

Add the following lines to the /etc/mail/local-host-names file. this will tell Sendmail to accept email addressed to this domain as well as the specified hosts which are all aliases for the mail server.

example.com
hostname.example.com
mail.example.com

Relay

Add the following line to the access database file, /etc/mail/access. this allows hosts on the 192.168.56.0/24 network to relay email through this mail server. By limiting the IP addresses to those of our network, spammers are unable to use our mail server to relay spam for them. Without this limitation, we would be running an “open relay” and the Ip address of our server would be blocked by many legitimate email systems.

192.168.56 RELAY

In debian you probably will find something like below:

Connect:192.168 RELAY
Make – Compile the configuration file

PWd to /etc/mail as the, run the make command. the make command runs the instructions required to convert the various text configuration files we have modified into the database files in the proper formats needed by Sendmail.

# make

Verify that the timestamps for the *.db files that correspond to the altered files have been changed by ls -fl.

 

FQDN

Before restart the service, I want to check if anything going wrong, so open another terminal session and run below command:

tail -f   /var/log/mail.log

Now Restart the service:

systemctl restart sendmail 

You may see error message:

xxxx sendmail[6078]: My unqualified host name (xxxx) unknown; sleeping for retry .

This error occurs because we set the virtual machines’s host name without using the fully qualified domain name (FQDN).

Type command:

hostnamectl

Static hostname: xxxx
Icon name: computer-vm
Chassis: vm
Machine ID: 7d6d27987bd54cf59b6ea178c3e4cf92
Boot ID: 90a39e45276a40a38fea6e16d91029f0
Virtualization: microsoft
Operating System: Ubuntu 18.04.4 LTS
Kernel: Linux 5.3.0-1020-azure
Architecture: x86-64

To add the FQDN to sendmail where xxxx is your machine hostname:

hostnamectl set-hostname xxxx.example.com

also check the /etc/hostname file which is where the hostname is stored.

Now the server should be functioning regardless of the security features such as spam check and authorization.

Test with mailx

We will create a user frank on local machine and test with the simplest email first:

useradd frank

passwd frank

We will use the mailx command to test a local email for us, enter the following command. the -s option of the mailx command sets the subject text – in double quotes – of the email.

echo "Hello world" | mailx -s "test mail again"  frank@example.com

Open another terminal session, and login with user frank.

$ mailx
"/var/mail/frank": 1 message 1 new
>N 1 root Tue May 5 09:25 15/717 test mail again
?

Hit enter

Return-Path: <root@example.com>
Received: from example.com (localhost [127.0.0.1])
by example.com (8.15.2/8.15.2/Debian-10) with ESMTP id 044NPqQ1011868
for <frank@example.com>; Tue, 5 May 2020 09:25:52 +1000
Received: (from root@localhost)
by example.com (8.15.2/8.15.2/Submit) id 044NPq2l011867;
Tue, 5 May 2020 09:25:52 +1000
Date: Tue, 5 May 2020 09:25:52 +1000
From: root <root@example.com>
Message-Id: <202005042325.044NPq2l011867@example.com>
Subject: test mail again
To: <frank@example.com>
X-Mailer: mail (GNU Mailutils 3.4)

Hello world

That worked and you could use q<Enter> to quit from mailx . Then you should see below:

Saved 1 message in /home/frank/mbox
Held 0 messages in /var/mail/frank
you have mail

 

notice that there are very few headers because the message was delivered by the email server on the local host