Configure sendmail on CentOS to allow sending of email from localhost

For some of my Drupal sites and PHP scripts (and shell scripts) that I run on a VPS I manage, I need to simply be able to send outgoing emails from arbitrary email addresses. I could go into all the details of DNS SPF records and MX records here, but that's something you'll need to research on your own. This post simply shows how to install and configure sendmail on a CentOS box to just allow outgoing mail from php's mail() function, the mail command line utility, etc., and only from localhost (127.0.0.1):

First, install sendmail with $ sudo yum install sendmail sendmail-cf.

Then, configure sendmail by editing the file /etc/mail/sendmail.mc (don't edit the sendmail.cf file - we'll auto-generate that after setting things correctly in sendmail.mc).

  • Configure DAEMON_OPTIONS to only allow sending from localhost/smtp: DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
  • Set the LOCAL_DOMAIN option to your hostname: LOCAL_DOMAIN(`example.com')dnl

Now, to update sendmail's configuration, enter $ sudo make -C /etc/mail, and then restart sendmail with $ sudo service sendmail restart.

You'll also want to make sure your hostname is set correctly (this will be the default from address domain); check it by entering $ hostname... if it's incorrect, you can set it explicitly in /etc/sysconfig/network as the HOSTNAME variable. You might also want to add your hostname to /etc/hosts as the first result for 127.0.0.1.

(This guide assumes you're using RHEL or CentOS/Fedora...).

Comments

thnx, simple and useful. this helped me seting up a development lamp stack!

Thank you, Its working.
It is very easy & simple to understand/implement.

Thank you for this tuto.
I had tryed before SendMail, I had to do "alternatives --config mta" and choose Ssmtp service to use it (Centos7).

Thank you so much.
This was the only guide I found that dealt with the localhost itself in a simple way.

2020. Still works like a charm with CentOS 7. Thanks!

Good day fello CentOS users. I am using version 8 and this does not seem to be working for me. Could this be handled differently in version 8?

What if I have multiple domains using this feature? How can I set the LOCAL_DOMAIN to handle that?

Currently my sendmail.mc file shows this: LOCAL_DOMAIN(`localhost.localdomain')dnl

Rogers canada has port 25 blocked for me :(

Still notice that this is not user friendly, what if we do not have a domain name to use? Is there another choice for LOCAL_DOMAIN that can be used. Somrthing a regular JOE would understand?

wtf is this tutorial? give an example at least.

Well, everything works at first, but when sendmail tries to relay the messages to their destination domains, they hit a timeout and the delivery fails.
These are all outgoing messages containing server information, say, like a status or a system problem. Opened inbound traffic for ports 25 and 465 but the handshaking still fails, timeout. The host sending the mail has outbound access to everywhere. But the messages don't get out.