Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Connection could not be established with host smtp and a different port from 465 with tls enabled #40887

Closed
nicolalazzaro opened this issue Feb 9, 2022 · 19 comments
Assignees

Comments

@nicolalazzaro
Copy link

  • Laravel Version: 9.0.0
  • PHP Version: 8.1.1
  • Database Driver & Version:

Description:

I have created a new application and receive the following linked error when sending an email.

With the same smtp parameters the error does not occur on the same machine but with laravel version 8.83.0.

I then created a new project using the new library "symfony/mailer" but the sending of the email is successful.

Flare error url: https://flareapp.io/share/B5ZYQWg7#F55.

@driesvints
Copy link
Member

@nicolalazzaro how did you solve this?

@nicolalazzaro
Copy link
Author

@nicolalazzaro how did you solve this?

I'm working on it, it's a problem with the certificate of authority for my machine.
For this reason, the error is not related to Laravel, so I have "closed" this issue.

@wfern
Copy link

wfern commented Feb 9, 2022

@nicolalazzaro please, if you solve this issue, report it here.

@mattkingshott
Copy link
Contributor

I can confirm this happens to me too.

@driesvints
Copy link
Member

I'll have a look tomorrow.

@nicolalazzaro
Copy link
Author

nicolalazzaro commented Feb 9, 2022

Comparing the arguments of the vendor/symfony/mailer/Transport/Smtp/Stream/SocketStream.php class.
What I noticed is that the argument "SocketStream->$tls" is true even when the port is not the correct one, as in the comment below "465".
As a result the prefix ssl:// is added to the host and this generates the above error.

In the class vendor/symfony/mailer/Transport/Smtp/EsmtpTransport.php starting on line 48 there is a piece of code that may be useful in solving the problem.

With port 465 and the argument "SocketStream->$tls" set to true the host in my case becomes "ssl://smtp.gmail.com:465" and the connection is successful, so the email is sent correctly.

Laravel
Create a new Laravel v9.0.0 project

CASE 1
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_ENCRYPTION=tls

In the above class to the function "initialize" the arguments are as follows:

#stream: null
#in: null
#out: null
-debug: ""
-host: "smtp.mailtrap.io".
-port: 2525
-tls: true
-sourceIp: null
-streamContextOptions: []

CASE 2
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_ENCRYPTION=tls

In the above class to the "initialize" function the arguments are as follows:

#stream: null
#in: null
#out: null
-debug: ""
-host: "smtp.gmail.com".
-port: 465
-tls: true
-sourceIp: null
-streamContextOptions: []

Blank project
Create a blank project and install the "symfony/mailer" package

CASE 1
smtp://username:password @ smtp.mailtrap.io:2525

In the above class to the function "initialize" the arguments are as follows:

#stream: null
#in: null
#out: null
-debug: ""
-host: "smtp.mailtrap.io"
-port: 2525
-tls: false
-sourceIp: null
-streamContextOptions: []

CASE 2
smtp://username:password @ smtp.gmail.com:465

In the above class to the function "initialize" the arguments are as follows:

#stream: null
#in: null
#out: null
-debug: ""
-host: "smtp.ionos.it"
-port: 465
-tls: true
-sourceIp: null
-streamContextOptions: []

Here is a similar case history of the error and useful information: symfony/symfony#34846 (comment)

@nicolalazzaro nicolalazzaro changed the title [9.x] Connection could not be established with host smtp [9.x] Connection could not be established with host smtp and a different port from 465 Feb 9, 2022
@nicolalazzaro nicolalazzaro changed the title [9.x] Connection could not be established with host smtp and a different port from 465 [9.x] Connection could not be established with host smtp and a different port from 465 with tls enabled Feb 10, 2022
@driesvints
Copy link
Member

Can everyone confirm they have their ports/SSL settings set correctly?

@mattkingshott
Copy link
Contributor

I have this:

'smtp' => [
    'transport'  => 'smtp',
    'host'       => env('MAIL_HOST', 'smtp.mailtrap.io'),
    'port'       => env('MAIL_PORT', 2525),
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username'   => env('MAIL_USERNAME'),
    'password'   => env('MAIL_PASSWORD'),
    'timeout'    => null,
],

I attempted ports 25, 2525, 465 and 587.

@nicolalazzaro
Copy link
Author

I have this:

'smtp' => [
    'transport'  => 'smtp',
    'host'       => env('MAIL_HOST', 'smtp.mailtrap.io'),
    'port'       => env('MAIL_PORT', 2525),
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username'   => env('MAIL_USERNAME'),
    'password'   => env('MAIL_PASSWORD'),
    'timeout'    => null,
],

I attempted ports 25, 2525, 465 and 587.

Does sending the email work if you set the MAIL_ENCRYPTION option to null?

@driesvints
Copy link
Member

@nicolalazzaro seems that does it for me. encryption needs to be null when using 2525 as a port for Mailtrap.

@driesvints
Copy link
Member

I'm going to close this as there's an easy fix to the problem. We don't document port 2525 anywhere anymore it seems. I see the default settings from Mailtrap are incorrect as they recommend setting encryption to tls for port 2525. I suggest you contact them to fix that.

Thanks all.

@nicolalazzaro
Copy link
Author

@nicolalazzaro seems that does it for me. encryption needs to be null when using 2525 as a port for Mailtrap.

This was not the case in the past, in fact if you copy the configuration from mailtrap it suggests that you set "tls" in the MAIL_ENCRYPTION option.

I think the problem lies in the interfacing with the new symfony library.

In @fabpot's comment below there should be the reason.
symfony/symfony#34846 (comment)

@driesvints
Copy link
Member

in fact if you copy the configuration from mailtrap it suggests that you set "tls" in the MAIL_ENCRYPTION option.

This seems wrong to me

@nicolalazzaro
Copy link
Author

nicolalazzaro commented Feb 10, 2022

in fact if you copy the configuration from mailtrap it suggests that you set "tls" in the MAIL_ENCRYPTION option.

This seems wrong to me

If you use the symfony/mailer package, SocketStream class added the prefix "ssl//:" to the host only with port 465, otherwise it is not added and the "tls" connection uses "starttls".

When using a different port it is not enabled to use the "ssl/:" prefix on the host.

This is what I was able to figure out.

Schermata 2022-02-10 alle 14 23 43

@driesvints
Copy link
Member

Yeah this seems like a change in behavior in Symfony Mailer to me. We'd welcome PR's to add something to the upgrade guide 👍

@nicolalazzaro
Copy link
Author

@driesvints In Laravel 9 if you dump $this->stream->isTLS() in the first line before vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php:252 you will always get true, even if the port is different from 465.

If you create an empty project and install the symfony/mailer package this does not happen, if the port is different from 465 then $this->stream->isTLS() returns false.

Around here should be the problem, unfortunately I'm in the office now and have no way of investigating.

@driesvints
Copy link
Member

@nicolalazzaro hmm odd. Would be happy to see what your findings are here...

nicolalazzaro pushed a commit to nicolalazzaro/laravel-framework that referenced this issue Feb 10, 2022
@nicolalazzaro
Copy link
Author

@driesvints this solves the problem, take a look #40943

@nicolalazzaro
Copy link
Author

I have this:

'smtp' => [
    'transport'  => 'smtp',
    'host'       => env('MAIL_HOST', 'smtp.mailtrap.io'),
    'port'       => env('MAIL_PORT', 2525),
    'encryption' => env('MAIL_ENCRYPTION', 'tls'),
    'username'   => env('MAIL_USERNAME'),
    'password'   => env('MAIL_PASSWORD'),
    'timeout'    => null,
],

I attempted ports 25, 2525, 465 and 587.

Port 465 should work correctly though, have you tried using the package "symfony/mailer" outside of laravel and testing sending the email? Otherwise it is a problem related to the configuration of your machine, you can also try gmail smtp which allows you to connect with port 465.

taylorotwell pushed a commit that referenced this issue Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants