stream_socket_enable_crypto(): Peer certificate CN=`<hostname>' did not match expected CN=`smtp-relay.sendinblue.com'
Stefan Bogdanescu
Founder & Senior Architect
Decoding the SMTP Error: Fixing stream_socket_enable_crypto() in Laravel Mail
As senior developers, we often deal with seemingly simple configuration errors that hide deep networking or security issues. Today, we are diving into a frustrating error encountered when setting up email functionality within a Laravel application, specifically dealing with external SMTP services like Sendinblue. The error message, stream_socket_enable_crypto(): Peer certificate CN=<hostname>' did not match expected CN=smtp-relay.sendinblue.com`, points directly to an SSL/TLS certificate validation failure during the connection handshake.
This post will dissect why this error occurs in your Laravel setup and provide a comprehensive, developer-focused solution.
Understanding the Certificate Mismatch Error
The error stream_socket_enable_crypto(): Peer certificate CN=<hostname>' did not match expected CN=smtp-relay.sendinblue.com` is not a Laravel configuration mistake; it is an operating system and SSL layer communication failure.
When your application (via PHP) attempts to establish a secure connection (TLS) with the SMTP server (smtp-relay.sendinblue.com), the server presents an SSL certificate to prove its identity. Your client (the PHP process) then checks this certificate against the hostname it intended to connect to.
The error means: The hostname you are connecting to does not match the Common Name (CN) or Subject Alternative Name (SAN) listed on the certificate presented by the server.
In essence, your system is trying to talk to smtp-relay.sendinblue.com, but the certificate it received is issued for a different domain, causing the handshake to fail immediately.
Reviewing Your Laravel Mail Configuration
Let's examine the configuration you provided:
.env:
MAIL_MAILER=smtp
MAIL_HOST=smtp-relay.sendinblue.com
MAIL_PORT=587
MAIL_USERNAME=MAIL
MAIL_PASSWORD=*************
MAIL_ENCRYPTION=tls
config/mail.php: (The structure shown is standard and correctly maps the environment variables.)
Structurally, your Laravel mail configuration appears perfectly sound. The issue arises after Laravel passes these settings to the underlying PHP stream functions to initiate the network connection. Therefore, the focus must shift from Laravel code to the server and network environment.
The Real Fix: Addressing Server-Side Certificate Issues
Since the problem lies in the certificate validation, we need to investigate the environment where your application is running (your live server) and how it interacts with Sendinblue's infrastructure.
Here are the most common causes and solutions for this specific error:
1. Incorrect Hostname Usage
Ensure that MAIL_HOST exactly matches the hostname expected by the certificate authority. While smtp-relay.sendinblue.com seems correct, sometimes network configurations or proxies interfere with how the hostname is interpreted during the TLS handshake.
Action: Double-check if you are using an IP address instead of a DNS name, and ensure that all internal network routes permit the necessary certificate information to be passed correctly.
2. Intermediate Certificate Issues (The Most Likely Culprit)
This error frequently occurs when the server chain—the path from the final certificate back to the root Certificate Authority (CA)—is incomplete or improperly configured on the server hosting the SMTP relay. If your live server environment is missing necessary intermediate certificates, it cannot fully validate the connection against Sendinblue's certificate, leading to the mismatch error.
Action: You need to ensure that the server running PHP has access to a complete and trusted set of root and intermediate certificates. This often involves updating system packages or ensuring your web server configuration (Apache/Nginx) is correctly handling SSL contexts for outbound connections.
3. Network Proxy Interference
If your application sits behind a corporate proxy, the proxy might be intercepting the TLS connection and presenting its own certificate, which causes a mismatch with the expected Sendinblue certificate.
Action: Test connectivity from the server environment directly (e.g., using openssl s_client commands) to isolate whether the issue is internal networking or an external certificate problem.
Conclusion: Building Robust Infrastructure
Dealing with these low-level networking errors is a crucial part of being a senior developer. While Laravel provides an elegant abstraction for sending mail, the underlying reliability depends entirely on the transport layer—the network security and certificate validation.
For future projects, always treat the communication channel as a separate entity from your application logic. As you build robust systems, focusing on secure, verifiable infrastructure is key. For further guidance on building scalable and reliable applications, I highly recommend exploring the best practices outlined by teams like those at laravelcompany.com. By verifying your server’s certificate chain and network configuration, you can resolve this specific error and ensure your Laravel mail service operates reliably in production.
Note: Blog content is currently available in English.