Problem Description
On a VPS environment deployed using the LNMP one-click installation package, the mail sending test in a website probe (e.g., phpinfo probe) fails, even though the probe shows the mail() function status as normal and the sendmail service is running.
Troubleshooting Steps
Follow these steps in order to diagnose and fix the issue.
1. Verify Sendmail Service Status
First, check if the sendmail service is installed and running.
service sendmail status
A normal output should resemble:
sendmail (pid 29294) is running...
sm-client (pid 29303) is running...
If the service is not running or not installed:
- CentOS/RHEL systems: Install using
yum install sendmail sendmail-cf -y. - Debian/Ubuntu systems: Install using
apt-get install sendmail -y. - After installation, start the service with
service sendmail start.
2. Core Fix: Configure PHP's Sendmail Path
This is the most common cause. Even if the service is running, PHP needs to know the exact path to the sendmail executable.
- Edit the PHP configuration file. Typically located at:
vi /usr/local/php/etc/php.ini - Search for the
sendmail_pathdirective:/sendmail_path - You will likely find a commented line:
;sendmail_path = - Remove the semicolon (
;) comment and set the path with parameters:sendmail_path = /usr/sbin/sendmail -t -i
Note:-ttells sendmail to extract recipients from the message body, and-iignores dots (.) on a line by themselves. - Save and exit the editor (in vi, press
Esc, then type:wqand press Enter).
3. Restart PHP-FPM Service
After modifying php.ini, you must restart the PHP processor.
/etc/init.d/php-fpm restart
Or, depending on your LNMP version:service php-fpm restart
orsystemctl restart php-fpm
After restarting, test the mail function again via the website probe. This usually resolves the issue.
Common Sendmail Service Management Commands
- Start service:
/etc/init.d/sendmail startorservice sendmail start - Stop service:
/etc/init.d/sendmail stoporservice sendmail stop - Restart service:
/etc/init.d/sendmail restartorservice sendmail restart - Check status:
service sendmail status
Additional Notes & Advanced Troubleshooting
If the problem persists after the steps above, consider the following:
- Firewall/Security Groups: Ensure outbound rules for port 25 (SMTP) are open on your server. Test external connectivity with
telnet smtp.gmail.com 25. - Hostname Configuration: An incomplete hostname can cause sendmail to refuse sending. Check the
/etc/hostsfile to ensure 127.0.0.1 maps to a correct fully qualified domain name (FQDN). - Mail Relay Restrictions: Many VPS providers block outbound connections on port 25 by default to prevent spam. You may need to contact your provider to unblock it or use their provided SMTP relay service.
- Alternative Solutions: For production environments, it's recommended to use an external SMTP service (like SendGrid, Mailgun) or configure modern mail server components like Postfix, and send mail via PHP libraries such as PHPMailer or SwiftMailer for greater reliability and deliverability.