PHPMailer generates PHP Warning: stream_socket_enable_crypto(): Peer certificate did not match expected

PHPMailer generates PHP Warning: stream_socket_enable_crypto(): Peer certificate did not match expected

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHPMailer generates PHP Warning: stream_socket_enable_crypto(): Peer certificate did not match expected

When upgrading from an older PHP environment to version 5.6, developers often encounter a wall of increased security strictness that breaks existing SSL/TLS connections. A common symptom is the error Could not connect to SMTP host from PHPMailer, even when raw logs show the connection was successfully established and the SMTP handshake proceeded normally.

In one specific case involving DreamHost's mail servers, the logs revealed a clear path: the client connected to mx1.sub4.homie.mail.dreamhost.com, received the ESMTP greeting, issued the EHLO command, and successfully initiated STARTTLS. The server responded with 220 2.0.0 Ready to start TLS, confirming the transition to a secure session was accepted by the remote end. However, immediately after this success, PHPMailer sent a QUIT command and abruptly closed the connection.

The root cause was exposed in the PHP warning:
stream_socket_enable_crypto(): Peer certificate CN=<code>*.mail.dreamhost.com' did not match expected CN=</code>mx1.sub4.homie.mail.dreamhost.com'

This discrepancy arises from how OpenSSL validates certificates during the TLS handshake. The server presents a certificate with the Common Name (CN) *.mail.dreamhost.com, which is a wildcard entry intended to cover multiple mail subdomains. However, PHP's default behavior in version 5.6 became stricter about matching the specific hostname you connected to against the certificate's CN or Subject Alternative Names (SANs). Since the connection was made to mx1.sub4.homie.mail.dreamhost.com, PHP rejected the certificate because it did not explicitly match that specific FQDN, despite the wildcard likely covering it in a broader context.

To bypass this validation and force the message through, developers often modify the SMTP options within PHPMailer. The configuration looks like this:

$mail->SMTPOptions = array (
    'ssl' => array(
        'verify_peer'      => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true));

By setting verify_peer to false, you instruct OpenSSL to skip the cryptographic verification of the certificate entirely. Setting verify_peer_name to false further relaxes the check, allowing connections even if the hostname doesn't strictly match the certificate's CN or SANs. This approach successfully silences the warning and allows the email to be sent.

However, disabling peer verification introduces significant security risks. Without this flag, your application is vulnerable to Man-in-the-Middle (MitM) attacks. An attacker positioned between your server and DreamHost could present a forged certificate that matches your hostname, and PHP would happily accept it because you told it not to verify the identity of the remote host.

If your goal is simply to trap the error while still sending the message, you need to separate the logic of "attempting to send" from "validating the connection." You cannot simply suppress the warning without configuring the options; PHPMailer throws an exception or halts execution before sending if verify_peer fails by default.

The only way to "trap" the error without disabling verification is to wrap the sending logic in a try-catch block that handles PHPMailerException. However, if the connection is closed due to the certificate mismatch, PHPMailer may not even reach the stage of throwing a catchable exception before the stream is closed.

In production environments, relying on wildcard certificates where the specific hostname doesn't match is often a sign of misconfiguration on the mail server side or an outdated certificate setup. The ideal solution is to configure the remote mail server (in this case, DreamHost) to present a certificate that includes the specific domain name in its Subject Alternative Names (SANs), rather than relying solely on the CN. If you cannot influence the remote host's configuration, you must decide whether the risk of MitM attacks justifies disabling verify_peer. For internal tools where encryption integrity is not critical, skipping verification might be acceptable, but for public-facing applications, it should be avoided at all costs.

Our network