March 29th, 2017
If you are running a web server or a VPS, you may encounter the need of sending emails using your PHP application.
In the same way, if you are running a WordPress blog or you are using any kind of CMS and you would allow your visitors to send you emails using a Contact form (for example using the Contact Form 7 plugin for WordPress) you may need to install a simple program into your web server called sendmail.
«Sendmail is a general purpose internetwork email routing facility that supports many kinds of mail-transfer and delivery methods, including the Simple Mail Transfer Protocol (SMTP) used for email transport over the Internet.» via Wikipedia.
Sendmail can be simply installed using the package manager of your distribution.
Here are the instructions for installing Sendmail on CentOS 7.
In order to install sendmail on your CentOS 7 server, run the following command:
# yum install sendmail
Allowing your server to send emails
If you are using SELinux on your CentOS 7 server, you have to allow sendmail to send emails using the following command:
# setsebool -P httpd_can_sendmail=on
Send a test email using PHP
Enter the php interactive shell using this command:
In the interactive shell, paste the following line of code:
mail ('email@example.com', "Test email", "Test email from the Internet", null, "-f firstname.lastname@example.org");
Don’t forget to replace email@example.com with the receiver email address and firstname.lastname@example.org with the sender email address.
Viewing the sendmail log
In order to monitor the mail log, you can use this command:
Once sendmail is installed on your web server you can allow your users to contact you via email using a contact form. 🙂