When working with Laravel to send emails, you might encounter an error related to STARTTLS, such as:
Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1
This error typically indicates an SSL/TLS configuration issue when Laravel attempts to connect to your email server. Here's a step-by-step guide to diagnose and fix this error:
Start by ensuring that your email settings in the .env
file is correctly configured. Common settings include:
MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
smtp
for most configurations.587
for TLS or 465
for SSL.tls
for STARTTLS or ssl
for SSL.The error may be due to an invalid or expired SSL/TLS certificate on your email server. You can check the certificate using online tools like SSL Labs' SSL Test or by using the following command in your terminal:
openssl s_client -starttls smtp -crlf -connect smtp.example.com:587
Replace smtp.example.com
with your actual SMTP host.
Ensure that your server has up-to-date CA certificates. On Ubuntu, you can update the CA certificates with:
sudo apt-get update
sudo apt-get install --reinstall ca-certificates
Verify CA Bundle Path: Ensure PHP is configured to use the correct CA bundle. Check the openssl.cafile
and openssl.capath
settings in your php.ini
file:
[openssl]
openssl.cafile=/path/to/cacert.pem
openssl.capath=/path/to/cacert
[curl]
curl.cainfo=/path/to/cacert.pem
Enable debugging to get more detailed error messages. In your config/mail.php
file, you can set the log_channel
to a specific channel to capture the error logs:
'log_channel' => env('MAIL_LOG_CHANNEL', 'stack'),
Then, in your .env
file, set:
MAIL_LOG_CHANNEL=mail
This will log the detailed email errors to your logs/laravel.log
file.
If the issue persists, try using alternative ports or encryption methods. For instance, switch to port 465
with ssl
encryption:
MAIL_PORT=465
MAIL_ENCRYPTION=ssl
Ensure that your firewall or ISP is not blocking the SMTP ports. Sometimes, ISPs block port 587
or 465
. You can test the connectivity using telnet
:
telnet smtp.example.com 587
Replace smtp.example.com
with your SMTP server address. If you can’t connect, contact your hosting provider or ISP for assistance.
If you can access the email server logs, review them for any indications of what might be going wrong. These logs can provide more insights into the SSL/TLS handshake process and where it might be failing.
Use Laravel’s artisan
command to send a test email:
php artisan tinker
Mail::raw('Test email', function ($message) {
$message->to('[email protected]')->subject('Test Email');
});
Update Laravel: Ensure you are using the latest version of Laravel and its dependencies.
Check Firewall Settings: Ensure your server’s firewall is not blocking outbound connections on the email server’s port.
Debugging Logs: Check Laravel’s logs located in storage/logs/laravel.log
for more detailed error messages.
Subscribe to the Email Newsletter