Blog / Linux/ Troubleshooting Mail() Function Failure in LNMP One-Click Installations

Troubleshooting Mail() Function Failure in LNMP One-Click Installations

LNMP一键安装包邮件发送故障排查:解决探针Mail()函数测试失败问题

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.

  1. Edit the PHP configuration file. Typically located at:
    vi /usr/local/php/etc/php.ini
  2. Search for the sendmail_path directive:
    /sendmail_path
  3. You will likely find a commented line:
    ;sendmail_path =
  4. Remove the semicolon (;) comment and set the path with parameters:
    sendmail_path = /usr/sbin/sendmail -t -i
    Note: -t tells sendmail to extract recipients from the message body, and -i ignores dots (.) on a line by themselves.
  5. Save and exit the editor (in vi, press Esc, then type :wq and 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
or
systemctl 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 start or service sendmail start
  • Stop service: /etc/init.d/sendmail stop or service sendmail stop
  • Restart service: /etc/init.d/sendmail restart or service sendmail restart
  • Check status: service sendmail status

Additional Notes & Advanced Troubleshooting

If the problem persists after the steps above, consider the following:

  1. Firewall/Security Groups: Ensure outbound rules for port 25 (SMTP) are open on your server. Test external connectivity with telnet smtp.gmail.com 25.
  2. Hostname Configuration: An incomplete hostname can cause sendmail to refuse sending. Check the /etc/hosts file to ensure 127.0.0.1 maps to a correct fully qualified domain name (FQDN).
  3. 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.
  4. 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.

Post a Comment

Your email will not be published. Required fields are marked with *.