Unpacking "certificate verify failed" in Laravel 9 Email Configuration
Encountering the error Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1 is a classic signal that your application trusts a server it shouldn't, or vice versa. Specifically, the OpenSSL message error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed indicates that PHP's internal SSL verification process rejected the certificate presented by your SMTP server.
In your local environment, this likely works because mailtrap or your localhost setup defaults to trusting self-signed certificates or skips verification entirely depending on the library used. On your production host, however, you are attempting a strict handshake with your own SMTP provider. The fact that HTTPS on your site works confirms your server's web certificate is valid; the issue is isolated strictly to the TLS/STARTTLS tunnel established during the SMTP connection.
Misconfiguring Encryption and Port Types
A frequent source of this specific error is a mismatch between the port selected and the encryption method specified in your .env file. You mentioned setting ENCRYPTION TYPE to SSL while using port 587. This is often a configuration trap.
Port 587 is universally designed for SMTP with STARTTLS, which upgrades an unencrypted connection to TLS mid-stream. When you explicitly set the encryption to "SSL" in Laravel's mail configuration while targeting port 587, the underlying PHP stream socket might attempt a handshake that conflicts with how the specific mail server expects the STARTTLS upgrade to occur, or it attempts to verify the certificate against a chain that isn't properly trusted by the default PHP OpenSSL context.
If your SMTP provider requires SSL/TLS (secure) from the start, you should use port 465. If they require STARTTLS, you must use port 587 and set the encryption type in Laravel to tls or leave it unset depending on the specific driver version behavior, but never "ssl" if the server expects a plain upgrade on port 587.
The Root Cause: Certificate Verification Strictness
The error message explicitly states certificate verify failed. In local development, PHP often accepts invalid certificates because the default verification options are relaxed. On production servers, especially those running strict security policies or using bundled OpenSSL configurations that lack specific CA bundles for certain providers, verification is stricter.
If your SMTP server uses a self-signed certificate or a certificate issued by a Certificate Authority (CA) that is not in your server's trusted store, the connection will fail immediately upon receiving the certificate. This is distinct from a generic connection timeout; it is a cryptographic rejection.
To resolve this without compromising security if you are using a legitimate CA certificate, ensure that the verify_peer option in PHP allows the specific chain presented by the mail server. However, the most robust fix for production environments is to ensure your server can validate the public CAs.
Implementation Steps to Fix the Issue
Check the Encryption Type: Update your
.envfile to match the port exactly.MAIL_MAILER=smtp MAIL_HOST=your-smtp-host.com MAIL_PORT=587 MAIL_ENCRYPTION=tls MAIL_USERNAME=your-username MAIL_PASSWORD=your-passwordNote the change from
ssltotlswhen using port 587. If your provider requires SSL on port 465, keep encryption asssland update the port.Verify System CA Bundle: Ensure the PHP installation on your host has access to a valid CA bundle. You can test this by checking if
ca-certificatesare installed on your OS. If you are unsure, try pointing Laravel to a specific trusted certificate path or relying on the system default/etc/ssl/certs/ca-certificates.crt.Inspect Server Logs: The error is thrown by PHP's stream wrapper. Check your server logs (often found in
/var/log/php-fpm/error.logor your web server logs) for the raw OpenSSL error output. Sometimes the error details are truncated in the application log, but the system log reveals exactly which CA chain failed or if a hostname verification mismatch occurred.Restart Services: After changing
.envvariables or installing new CA certificates, you must restart your PHP-FPM or Apache/Nginx service to pick up the new SSL context configurations.
Why This Happens in Production vs. Local
The discrepancy between your local Mailtrap setup and your production SMTP is the key differentiator. Mailtrap often presents a certificate that your local development machine accepts by default, bypassing strict chain verification checks that are active on your live server. Your live server, however, adheres to strict RFC standards for TLS handshakes.
If you cannot change the SMTP provider's certificate (e.g., it is strictly self-signed), and you have verified that the hostname matches exactly (no typos in MAIL_HOST), the only remaining option is to disable peer verification locally on the server, which introduces security risks. The preferred path is always to ensure the server trusts the CAs issuing the provider's certificates or to switch to a port/encryption combination that aligns with your specific mail service's requirements.
For further guidance on setting up secure email services in Laravel ecosystems, resources like https://laravelcompany.com often provide detailed breakdowns of configuration nuances.
