How to Install & Configure Sendmail on Debian 9 (Stretch)

Sendmail is a MTA (Mail Transfer Agent) server used for transferring e-mail from between completely different hosts. Sendmail makes use of SMTP protocol. Many of the system administrators most well-liked to make use of Sendmail server as MTA than other MTAs. This tutorial lets you Install Sendmail on Debian 9 (Stretch) Linux system.

1. Install Sendmail

If you don’t have installed Sendmail utilizing the next command to install Sendmail with other required packages utilizing the apt package manager.

sudo apt update
sudo apt install sendmail sendmail-cf m4

2. Configure Sendmail Server

Now, execute the sendmailconfig command to finish the basic configuration.

sudo sendmailconfig

Choose all choices to ‘Y’ and press enter. Wait for the command end.

Your server is prepared for sending emails. You should use the Linux command line or PHP script to send emails.

3. Receive Incomming Emails

Edit /etc/mail/sendmail.mc file and comment below line to allow receiving an e-mail from anywhere. To comment a line in sendmail.mc, simply put dnl keyword at the beginning of the line.

dnl DAEMON_OPTIONS(`Family=inet,  Name=MTA-v4, Port=smtp, Addr=127.0.0.1')dnl
dnl DAEMON_OPTIONS(`Family=inet, Name=MSP-v4, Port=submission, M=Ea, Addr=127.0.0.1')dnl

Then add your domains to /etc/mail/local-host-names file.

cat /etc/mail/local-host-names

sisrv.net
mail.sisrv.net
localhost
localhost.localdomain

Now use m4 is a macro processor to compile the Sendmail configuration files. m4 is stream-based, that’s, it doesn’t understand about lines.

sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

Restart Sendmail service

sudo systemctl restart sendmail

Your system is prepared for incoming emails.

Using sendmail from the command line

The following are two examples on how to send email from the command line using sendmail.

Simple example

Once logged in to your web server via SSH, you can run the following command to send email:

[server]$ /usr/sbin/sendmail youremail@example.com
Subject: Test Send Mail
Hello World
control d (this key combination of control key and d will finish the email.)

The above example sends an email as follows:

  • The first line indicates the email address sendmail will send the email TO. 
  • The Subject is 'Test Send Mail'.
  • The body of the message says 'Hello World'.
  • When the message is received, the FROM email address will appear as your user@server. For example, username@sisrv.net

Adding To and From addresses

This example is the same as the one above, but adds a specific To and From address:

[server]$ /usr/sbin/sendmail youremail@example.com
To: differentemail@example.com
From: anyone@example.com
Subject: Test Send Mail
Testing sendmail
control d (this key combination will finish the email.)

The above example sends an email as follows:

  • The first line indicates the email address sendmail will send the email TO. 
  • The email you enter into the To field does not receive an email. It only shows as the To address when the actual recipient receives the email. The recipient is still the first email you enter on the first line when running the command, and can also be the same if you wish.
  • The From address is what you entered: anyone@example.com.
  • The Subject is what you entered: Test Send Mail.
  • The body of the message is what you entered: Testing sendmail.

Sendmail in a cgi script

You can also use sendmail in a script as well. This can be done in several languages. The following example shows a perl cgi script.

Create a file name perltest.pl with the following code and make sure the permissions on the file are set to 755 or it will not run:

Note how the To, From, and Subject fields do not end unless the line is ended with \n.

#!/usr/bin/perl
use strict;
my($mailprog) = '/usr/sbin/sendmail';
my($from_address) ='AnyoneYouWant@example.com';
my($to_address) ='email@example.com';
       open (MAIL, "|$mailprog -t $to_address") || die "Can't open $mailprog!\n";
       print MAIL "To: $to_address\n";
       print MAIL "From: $from_address\n";
       print MAIL "Subject: Hello, self. Nice to email myself.";
       print MAIL "This is still subject line.\n";
       print MAIL "This is the body of my message.\n";
       print MAIL "Pretty boring, yes?\n";
       print MAIL "Goodbye.\n";
       close (MAIL);

This example sends an email to email@example.com.

4. Configure Domain-based E-mail Routing

As we learn above that virtusertable file used for aliasing, permitting multiple virtual domains to be hosted on one machine.

  • 1. All emails addressed to @example.com domain delivered to help@mydomain.com
    @example.com help@mydomain.com
    
  • 2. All emails addressed to help@mydomain.com will ahead to local user jack.
    help@mydomain.com  jack
    
  • 3. All emails addressed to @mydomain.com will ahead to domain @otherdomain.com with corresponding usernames.
    @mydomain.com    %1@otherdomain.com
    
  • 4. All emails addressed to @otherdomain.com shall be rejected my mail server with acknowledging sender with the message
    @otherdomain.com 	 error:nouser User unknown
    

After making all modifications in virtusertable execute the next command to create an updated virtusertable.db file containing the new configuration.

sudo makemap hash /etc/mail/virtusertable < /etc/mail/virtusertable

Additionally ensure that the following entry is added in sendmail.mc file.

FEATURE(`virtusertable')

Add entry if not available and compile configuration file

sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

Now restart Sendmail service

sudo systemctl restart sendmail

Thanks for reading this article. I hope this article will show you how to to Install & configure Sendmail on Debian 9 (Stretch) system.

  • install sendmail, sendmail debian install
  • 4 Kunder som kunne bruge dette svar
Hjalp dette svar dig?

Relaterede artikler

How to Set Up a Mac for Your Kids

Providing children with access to a computer and the internet is increasingly important, but so...

How to Get Help With a Command from the Linux Terminal

Whether you’re an inexperienced terminal user or a grizzled veteran, you won’t always know the...

How to change the ssh port on Linux

To Change the SSH Port for Linux Server Connect to your server via SSH Switch to the root...

How To Install screenFetch in Linux

If you've browsed Linux groups on social media, you've probably seen a lot of screenshots that...

Static IP vs. Dynamic IP Address

A static IP address is one that remains fixed and never changes. The PC always sees the same...

Powered by WHMCompleteSolution