CentOS Install Sendmail

Sendmail is a MTA (Mail Transfer Agent) server used for transferring email from between different hosts. Sendmail uses SMTP (Simple Mail Transfer Protocol) protocol. Most of system administrators preferred to use Sendmail server as MTA than other apps.

RHEL 5 or its earlier versions were using Sendmail as default mail server, But newer version’s of RHEL based systems adapted postfix as default mail server. Most of users are familiar with Sendmail and want to use it with version 6 also. This article will help that users for installing Sendmail server on RHEL 7/6/5 or with minimal configuration.

How Sendmail Works

Incoming Mail

Usually each user in your home has a regular Linux account on your mail server. Mail sent to each of these users (username@xyz.com) eventually arrives at your mail server and sendmail then processes it and deposits it in the mailbox file of the user’s Linux account. Mail isn’t actually sent directly to the user’s PC.

Users retrieve their mail from the mail server using client software, such as Microsoft’s Outlook or Outlook Express, that supports either the POP or IMAP mail retrieval protocols. Linux users logged into the mail server can read their mail directly using a text-based client, such as mail, or a GUI client, such as Evolution. Linux workstation users can use the same programs to access their mail remotely.

Outgoing Mail

The process is different when sending mail via the mail server. PC and Linux workstation users configure their e-mail software to make the mail server their outbound SMTP mail server. If the mail is destined for a local user in the “xyz.com” domain, then sendmail places the message in that person’s mailbox so that they can retrieve it using one of the methods above.

If the mail is being sent to another domain, sendmail first uses DNS to get the MX record for the other domain. It then attempts to relay the mail to the appropriate destination mail server using the Simple Mail Transport Protocol (SMTP). One of the main advantages of mail relaying is that when a PC user A sends mail to user B on the Internet, the PC of user A can delegate the SMTP processing to the mail server.

Sendmail on CentOS

Installing Sendmail on CentOS

Check if sendmail is installed or not:

# rpm –qa | grep sendmail

If you don’t have installed Sendmail using following command to install Sendmail with other required packages using yum package manager.

# yum install sendmail sendmail-cf m4

Configure Sendmail Server

Before starting configuration we must know about various Sendmail configuration files exists in /etc/mail directory.

  • access — Allow/Deny other systems to use Sendmail for outbound emails.
  • domaintable — Specifies domain name mapping for Sendmail.
  • local-host-names — Specifies aliases for the host.
  • mailertable — Specifies instructions that override routing for particular domains.
  • virtusertable — Specifies a domain-specific form of aliasing for hosting multiple virtual domains on one machine.

Comment out below line in /etc/mail/sendmail.mc to allow receiving email from anywhere. To comment a line in sendmail.mc, just put dnl at start of line.

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

Add this line also in sendmail.mc above ‘MAILER’ option

FEATURE(`relay_hosts_only’)dnl

Add your PC’s full hostname in this file.

# hostname >> /etc/mail/relay-domains

Recompile Sendmail Configuration

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

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

Restart Sendmail service

# /etc/init.d/sendmail restart

Configure Domain based E-mail Routing

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

  1. All emails addressed to @example.com domain delivered to support@mydomain.com

    @example.com support@mydomain.com

  2. All emails addressed to support@mydomain.com will forward to local user jack.

    support@mydomain.com jack

  3. All emails addressed to @mydomain.com will forward to domain @otherdomain.com with corresponding usernames.

    @mydomain.com %1@otherdomain.com

  4. All emails addressed to @otherdomain.com will be rejected my mail server with acknowledging sender with message

    @otherdomain.com error:nouser User unknown

After making all changes in virtusertable execute following command to create updated virtusertable.db file containing the new configuration.

# makemap hash /etc/mail/virtusertable < /etc/mail/virtusertable

Now restart Sendmail service

# /etc/init.d/sendmail restart

Firewall Rule (Optional)

Firewall port that needs to be open for sendmail daemon machine:

# iptables -A INPUT -p tcp -m tcp –dport 25 -j ACCEPT

Save the Iptables rules and restart it.

service iptables save

service iptables restart

Congratulation you have successfully setup MTA service using Sendmail.

Testing eMail Server

To test your server to see if the web service can send out emails you can enter into the php interactive shell and issue the mail command. To enter into the php interactive shell enter the following command: php -a

Once in the php interactive mode enter the following command into you terminal, changing the email address in red to the destination email:

mail("to@email.com", "Testing Mail", "Hello from CentOS", null, "-f from@email.com");

To exit from the php interactive mode enter the following command exit. If you do not see an email in your inbox, be sure to check the spam or junk folders.