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

Mailer Transports to SMTP Cannot Establish Secure Connections #34846

Closed
brandonsavage opened this issue Dec 6, 2019 · 15 comments
Closed

Mailer Transports to SMTP Cannot Establish Secure Connections #34846

brandonsavage opened this issue Dec 6, 2019 · 15 comments

Comments

@brandonsavage
Copy link

Symfony version(s) affected: 5.0.1

Description
When using the Transport out of the box, the Transport is unable to establish a secure connection. This issue looks similar to #34064

I am using OpenSSL 1.1.1d and PHP 7.4. I've reproduced the issue on PHP 7.3 as well.

The exact error message I receive is:

Connection could not be established with host "ssl://smtp.postmarkapp.com:587": stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number

How to reproduce

$transport = new PostmarkSmtpTransport($key);
$mailer = new Mailer($transport);
$mail = (new Email)->to('test@example.com')
->from('test@example.com')
->subject('Test Email')
->html('Test Email');
$mailer->send($mail);
@fabpot
Copy link
Member

fabpot commented Dec 7, 2019

maybe fixed by #34760?

@brandonsavage
Copy link
Author

I applied the patch in #34760 without success in resolving this bug.

@kbsanders
Copy link

I am experiencing this exact same issue using the following SMTP transport:

$transport = new EsmtpTransport('smtp.office365.com',587,true);

The error received is:

Connection could not be established with host "ssl://smtp.office365.com:587": stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number.

I am using Symfony version 5.0.1 and PHP version 7.3.11.
I am also using the localhost Symfony web server for development.

The patch mentioned above did not help to correct the issue.

@kbsanders
Copy link

Update:

I ran my code again, but this time with the TLS option set to false and the patch above applied and the transport actually succeeded.

$transport = new EsmtpTransport('smtp.office365.com',587,false);

However, when the patch is not applied, and TLS = false, I receive the following error:

Warning: fgets(): SSL: An existing connection was forcibly closed by the remote host. in C:\arwa\site-alruralwater\vendor\symfony\mailer\Transport\Smtp\Stream\AbstractStream.php (line 77)

Does this working with TLS = false perhaps have something to do with TLS vs STARTTLS?

@toooni
Copy link
Contributor

toooni commented Dec 22, 2019

@kbsanders But is the mail actually being sent? I can confirm that the error persists even with #34760. Setting TLS to false does indeed not result in the mentioned error and everything seems fine at first. But no mail is sent (no incoming mail on gmail nor activity visible on postmark).

@kbsanders
Copy link

@toooni I believe it was working for me, but I ended up switching to Swift Mailer for the time being until Mailer matures a bit more. I could never get Mailer to work with TLS. Swift Mailer has no issues with TLS.

@fabpot
Copy link
Member

fabpot commented Jan 30, 2020

To be clear, you should use 465, true to use SSL to connect or 587, false to connect withou SSL and upgrade via STARTTLS. Any other combination won't work. The code was buggy, fixing it here:

@fabpot fabpot closed this as completed Jan 30, 2020
fabpot added a commit that referenced this issue Jan 30, 2020
…abpot)

This PR was merged into the 4.4 branch.

Discussion
----------

[Mailer] Fix STARTTLS support for Postmark and Mandrill

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #34846 <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | n/a

Commits
-------

44b27c6 [Mailer] Fix STARTTLS support for Postmark and Mandrill
@cengizgoren
Copy link

To be clear, you should use 465, true to use SSL to connect or 587, false to connect withou SSL and upgrade via STARTTLS. Any other combination won't work. The code was buggy, fixing it here: @fabpot you're a genius. ..

this solved my problem. i even told this solution to one.com support . thanks again

@bestvayne
Copy link

To be clear, you should use 465, true to use SSL to connect or 587, false to connect without SSL and upgrade via STARTTLS. Any other combination won't work. The code was buggy, fixing it here:

yes, 465 is right , I use mailgun service in Laravel 7.X, the error is "Connection could not be established with host smtp.mailgun.org:stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number"

and I use 465 replace Laravel init 587, the mistake was solved.

gbirke added a commit to wmde/fundraising-application that referenced this issue Sep 29, 2021
This will make the mailer "auto-discover" the encryption capabilities of
the SMTP server, allowing us to use port 587 with STARTTLS negotiation.
Another solution would be to use port 465 with TLS set to `true`,
but the [recommended way][1] is to use port 587 and let the library
negotiate the encryption.

See also symfony/symfony#34846 (comment)

[1]: https://sendgrid.com/blog/whats-the-difference-between-ports-465-and-587/
@hanzallahmsd
Copy link

To be clear, you should use 465, true to use SSL to connect or 587, false to connect withou SSL and upgrade via STARTTLS. Any other combination won't work. The code was buggy, fixing it here:

I am also getting the same error. Where I will put these values?

@hanzallahmsd
Copy link

My SMTP Settings look like this

MAIL_MAILER=smtp
MAIL_HOST=smtpout.secureserver.net
MAIL_PORT=587
MAIL_USERNAME=me@mysite.com
MAIL_PASSWORD=MyPassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=hanzallah@mysite.com
MAIL_FROM_NAME="${APP_NAME}"

@hanzallahmsd
Copy link

Pasting some part of the error.

[2022-08-27 08:05:13] local.ERROR: Connection could not be established with host "ssl://smtpout.secureserver.net:465": stream_socket_client(): Unable to connect to ssl://smtpout.secureserver.net:465 (Connection timed out) {"exception":"[object] (Symfony\Component\Mailer\Exception\TransportException(code: 0): Connection could not be established with host "ssl://smtpout.secureserver.net:465": stream_socket_client(): Unable to connect to ssl://smtpout.secureserver.net:465 (Connection timed out) at /var/www/mysite.com/vendor/symfony/mailer/Transport/Smtp/Stream/SocketStream.php:154)
[stacktrace]
#0 [internal function]: Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream->Symfony\Component\Mailer\Transport\Smtp\Stream\{closure}()
#1 /var/www/mysite.com/vendor/symfony/mailer/Transport/Smtp/Stream/SocketStream.php(157): stream_socket_client()
#2 /var/www/mysite.com/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(253): Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream->initialize()
#3 /var/www/mysite.com/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(192): Symfony\Component\Mailer\Transport\Smtp\SmtpTransport->start()
#4 /var/www/mysite.com/vendor/symfony/mailer/Transport/AbstractTransport.php(68): Symfony\Component\Mailer\Transport\Smtp\SmtpTransport->doSend()
#5 /var/www/mysite.com/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(136): Symfony\Component\Mailer\Transport\AbstractTransport->send()
#6 /var/www/mysite.com/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(521): Symfony\Component\Mailer\Transport\Smtp\SmtpTransport->send()
#7 /var/www/mysite.com/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(285): Illuminate\Mail\Mailer->sendSymfonyMessage()

@bitfactory-edo-freriks
Copy link

@hanzallahmsd did you try

MAIL_ENCRYPTION=starttls

@maynor96
Copy link

maynor96 commented Feb 23, 2023

starttls

it worked for me!

@prodev711
Copy link

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=myuser
MAIL_PASSWORD=mypass
MAIL_ENCRYPTION=STARTTLS

When I set env values like this, there is no error. but email not send to target email.

nicolas-grekas added a commit that referenced this issue Dec 19, 2023
…e STARTTLS (RFreij)

This PR was merged into the 6.4 branch.

Discussion
----------

[Mailer] [Mailjet] Disable tls for mailjet as it should use STARTTLS

| Q             | A
| ------------- | ---
| Branch?       | 6.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        |  <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead -->
| License       | MIT

#52560 updates the default mailjet port and references the same change for mailgun.

Similar to #51042 and with the same error message, the mailjet connection doesn't work now.

The PR above already has a reference, but for completion sake: Ref #34846 (comment) for more explanation

Commits
-------

709d56f fix(mailjet-smtp): disable tls for mailjet as it should use starttls instead
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