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:
- Set the LOCAL_DOMAIN option to your hostname:
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...).