If your PHPMailer script or any CMS (like Joomla) using PHPMailer is unable to send messages on a Plesk server, this may be due to SSL/TLS certificate verification settings in PHP. Beginning with PHP 5.6, PHP verifies peer certificates and hostnames by default, which can cause connection issues if the certificate configuration does not match expected requirements.
Symptoms
When attempting to send messages via SMTP in applications like PHPMailer or Joomla, you may encounter one of the following issues:
- Unable to configure SMTP server in Joomla
- SMTP Error: Could not connect to SMTP host
Additionally, the following error may appear in the domain’s error log (/var/www/vhosts/system/example_domain.com/logs/error_log
):
mod_fcgid: stderr: Connection failed. Error #2: stream_socket_enable_crypto(): Peer certificate CN=`example_domain.com' did not match expected CN=`hostname.com'
Cause
This error occurs because, starting from PHP 5.6, PHP’s stream wrapper enforces peer certificate and hostname verification when using SSL/TLS connections. This means that:
- The connection will fail if a self-signed certificate is used.
- The connection will also fail if the SSL certificate’s Common Name (CN) does not match the domain name used for the SMTP connection.
Resolution
To resolve this issue, it is necessary to secure the mail server with a valid SSL certificate that matches the domain name. Here’s how:
- Obtain an SSL Certificate
Acquire a valid SSL certificate for the mail server. You can use a commercial SSL certificate or a free Let’s Encrypt certificate, depending on your requirements. - Configure the Mail Server to Use the SSL Certificate
In Plesk, configure the mail server to use the SSL certificate for encrypted connections:- Go to
Tools & Settings > Mail Server Settings
. - Select the SSL certificate that matches the mail server’s hostname (e.g.,
hostname.com
). - Save the changes to apply the SSL certificate to the mail server.
- Go to
- Update PHPMailer or CMS SMTP Settings
Update the SMTP settings in PHPMailer or your CMS to use the secure mail server hostname (e.g.,hostname.com
) that matches the SSL certificate.
Additional Notes
Using an SSL certificate that matches the mail server hostname ensures secure and verified connections, as required by PHP’s SSL/TLS verification settings starting with PHP 5.6. If you continue to use self-signed certificates, disable verification in PHPMailer, although this is not recommended for security reasons.
Disclaimer
For security, always use a valid SSL certificate from a trusted certificate authority. Avoid disabling verification settings, as this weakens the security of SMTP connections.