Email is an important part of modern web apps, and Laravel makes it easy by including the powerful Symfony Mailer. However, developers sometimes have trouble setting up SMTP services. One of these common mistakes is
"Can't connect to STARTTLS because the peer certificate hostname doesn't match."
When using secure SMTP providers like HIN.CH, this problem often happens. In this article, we'll explain what caused this error and show you how to fix it in Laravel step by step.
What does this mistake mean?
When your Laravel app tries to make a secure connection with STARTTLS but the SSL certificate from the mail server doesn't match the hostname you're connecting to, this error happens.
To put it simply:
Your app connects to an SMTP server, like mail.hin.ch.
The server shows an SSL certificate.
Laravel checks to see if the certificate hostname and the SMTP host are the same.
For security reasons, the connection is denied if it doesn't match.
This is a built-in safety feature that stops man-in-the-middle attacks.
Why does HIN.CH SMTP do this?
HINCH is a safe email service that is mostly used in healthcare settings. It has strict SSL/TLS security rules. Most of the time, the mismatch happens because of:
Your Laravel .env file has the wrong SMTP hostname.
Using an IP address instead of a domain name.
The certificate is set up for a different subdomain.
Problems with the local DNS or the server settings not being set up correctly.
Typical Signs
You might notice the following when this problem happens:
No emails are going out.
Laravel logs show errors with STARTTLS or SSL.
Problems with Symfony Mailer connections.
Errors with handshakes or timeouts.
Step-by-Step Fix
1. Verify SMTP Hostname
The first and most important thing to do is make sure that your SMTP host and certificate match.
In your .env file:
MAIL_MAILER=smtp
MAIL_HOST=mail.hin.ch
MAIL_PORT=
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your@email.com
MAIL_FROM_NAME="${APP_NAME}"
Make sure:
You are using the right hostname that HIN.CH gave you.
Don't use an IP address.
Don't use aliases unless the provider says it's okay.
2. Manually check the SSL certificate
You can check the certificate with OpenSSL:
connect mail.hin.ch:587 -starttls smtp with openssl s_client
Check for:
subject= field (should be the same as hostname)
issuer=
Warnings about any mismatches
Laravel will not let you connect if the certificate hostname does not match MAIL_HOST.
3. Change the settings for Laravel Mail
Symfony Mailer is what Laravel uses behind the scenes. In config/mail.php, you can change the settings.
If you need to, set up custom stream options:
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'encryption' => env('MAIL_ENCRYPTION'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'stream' => [
'ssl' => [
'verify_peer' => true,
'verify_peer_name' => true,
'allow_self_signed' => false,
],
],
],
],
This makes sure that strict verification is done, which is what is needed in production environments.
4. Temporary Workaround (Not Recommended for Production)
You can turn off hostname verification for a short time if you are debugging or testing on your own computer:
"stream" => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' =>
],
],
Warning:
This lowers security and should never be used in production.
5. Use Correct Port and Encryption
HIN.CH SMTP typically supports:
Port 587 → TLS (STARTTLS)
Port 465 → SSL
Ensure your configuration matches:
MAIL_PORT=
MAIL_ENCRYPTION=tls
If you switch to SSL:
MAIL_PORT=
MAIL_ENCRYPTION=ssl
Mismatch between port and encryption can also trigger errors.
6. Clear Laravel Cache
Always clear the cache after changing the settings:
php artisan config:clear
php artisan cache:clear
php artisan config:cache
Changes may not take effect right away because Laravel caches configuration.
7. Look into problems in your area
Sometimes the problem isn't with Laravel; it's with your computer:
CA certificates that are no longer valid.
The system time is wrong.
Firewall is blocking SMTP ports.
Antivirus is messing with SSL.
Refresh the certificates on your system:
update-ca-certificates with sudo
Details about the certificate.
They might give you a specific hostname that matches the SSL certificate.