Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

owen2jan's avatar

"Unable to connect with STARTTLS" error when sending email

I am using laravel 9 + mailgun to send emails. I get this error

Symfony\ Component\ Mailer\ Exception\ TransportException

Unable to connect with STARTTLS: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

config in .env

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=***
MAIL_PASSWORD=***
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=***
MAIL_FROM_NAME="${APP_NAME}"

config in config/mail

'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'auth_mode' => null,
            'local_domain' => env('MAIL_EHLO_DOMAIN'),
],

what could be the problem and how can I solve this?

0 likes
10 replies
owen2jan's avatar

@Nakov Followed the docs and added verify_peer' => false . I hadn't realized changes to symphony from swiftmailer , Im switching to laravel 9 ,thanks.

However, I'm getting a new error Unable to send an email: Forbidden (code 401)

1 like
Sinnbeck's avatar

@owen2jan if you had to disable the check if the certificate is valid to get it working, you should probably reach out to mailgun and inform them that their certificate is invalid. Forcing tls should fix it. I use mailgun myself on one of my projects and I can double check that I get a valid certificate there

Is there a known problem with mailgun that I am not aware of?

owen2jan's avatar

@Sinnbeck I think this is set as default already i.e encryption' => env('MAIL_ENCRYPTION', 'tls')

Sinnbeck's avatar

@owen2jan you are overwriting it with null. Set it in env or remove the line from env

owen2jan's avatar

@Sinnbeck Ah, I thought setting null would mean it takes the default in the config/mail. Makes sense just to remove it, thanks.

owen2jan's avatar

@Sinnbeck Also, I hadn't set up mailgun for laravel 9 as in the docs. done this, however now I get this error Unable to send an email: Forbidden (code 401)

Please or to participate in this conversation.