The following page explains how to install mail function on a CentOS server.
Step 1: Install the sendmail package #
- To install the sendmail package on a CentOS or Red Hat-based system, run the following command:
yum install sendmail
Step 2: Start and enable the sendmail service
- Start the sendmail service using the following command:
systemctl start sendmail
- Enable the sendmail service to start automatically on boot:
systemctl enable sendmail
Step 3: Edit the php.ini file
- Edit the php.ini file using a text editor such as vim:
vim /etc/php.ini
- Locate the following line in the php.ini file:
;sendmail_path =
- Uncomment the line by removing the semicolon (;) and set the sendmail_path to the path of the sendmail binary:
sendmail_path = /usr/sbin/sendmail -t -i
Save and close the php.ini file.
- Step 4: Restart the PHP-FPM service
Restart the PHP-FPM service to apply the changes made to the php.ini file:
systemctl restart php-fpm
After completing these steps, PHP’s mail() function should be enabled and configured to use the sendmail command to send emails.
To test the configuration, you can use a sample PHP script with the mail() function.
<?php $to = 'recipient@example.com'; $subject = 'Test email'; $message = 'This is a test email sent via PHP'; $headers = 'From: sender@example.com' . "\r\n" . 'Reply-To: sender@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); if (mail($to, $subject, $message, $headers)) { echo 'Email sent successfully'; } else { echo 'Email could not be sent'; } ?>
Ensure to replace recipient@example.com
and sender@example.com
with valid email addresses that you can access.
To access the PHP script through a web browser, use the URL http://yourdomain.com/path/to/file.php.
You will see the message “Email sent successfully” displayed in your browser if the email is sent successfully. If there are any errors, you will see the message “Email could not be sent”. In case of errors, it’s recommended to check the sendmail logs and PHP error logs for further information.
Following these steps, you can configure sendmail for PHP to send emails using the mail() function.
Thus we discussed how to install the mail function on a CentOS server.