I am trying to test a email connection with a user given configuration but I can't seem to find a way to test the connection. I get an error everytime I try to test it with a TLS encryption. If I change the port to a SSL one and the encryption to SSL it works, but I want to get it working with TLS too. I noticed that even when the port is set to the one TLS uses and the encryption is set to TLS it still adds "ssl://.." to the URL, I have no idea if this is what causes the problem or how to solve it.
I am using Laravel 10 and the Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport
class.
Really appreciate any help. Thanks in advance.
ssl://smtp.gmail.com:587": stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number
Here's how I try to test it:
// smtp.gmail.com
$host = Setting::where('key', 'email_host')->firstOrFail()->value;
// 587
$port = Setting::where('key', 'email_port')->firstOrFail()->value;
// my-username
$username = Setting::where('key', 'email_username')->firstOrFail()->value;
// my-app-password
$password = Setting::where('key', 'email_password')->firstOrFail()->value;
// TLS (or SSL)
$encryption = Setting::where('key', 'email_encryption')->firstOrFail()->value;
$tls = ($encryption == 'tls') ? true : false;
$transport = new EsmtpTransport($host, $port, $tls);
$transport->setUsername($username);
$transport->setPassword($password);
try { $transport->start(); }
catch (\Exception $e) { ... }
I think the parameter $tls
might be confusingly named.
SSL and TLS is synonym.
When setting $tls = true
indeed ssl://
is prefixed to the host, meaning a SSL encryption is used. See here in the code.
What you seem to try to do is to use STARTTLS
this happens automatically based on the port and if SSL is off.
See also https://talk.octobercms.com/t/smtp-starttls-and-mail-encryption-in-laravel-9/1041
So passing $tls = false
should do the job and use STARTTLS
.