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 this was straight forward and helped.

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).

Thanks! You make my day!

Thanks, this put me on the right track.
In the end, this post also helped me with this issue:
https://serverfault.com/a/241703/157994