Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mailer] Sendgrid mailer transport does not respect custom ports #36224

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

Closed
ftdysa opened this issue Mar 26, 2020 · 12 comments
Closed

[Mailer] Sendgrid mailer transport does not respect custom ports #36224

ftdysa opened this issue Mar 26, 2020 · 12 comments

Comments

@ftdysa
Copy link

ftdysa commented Mar 26, 2020

Symfony version(s) affected:
symfony/sendgrid-mailer v5.0.5

Description
The Sendgrid Smtp Transport does not respect custom ports provided in the DSN. From Sendgrid's documentation (https://sendgrid.com/docs/for-developers/sending-email/getting-started-smtp/), they suggest the following:

SendGrid accepts unencrypted and TLS connections on ports 25, 587, & 2525. You can also connect via SSL on port 465.

On inspection, it looks like this is how the various Smtp transports are setup so maybe this is an explicit design decision?

Also, it should be noted that you can get around this via something like:

$transport = Transport::fromDsn($dsn);
$transport->getStream()->setPort(587);

but this seems like it'd be nice for the library to handle for you since it already handles DSN parsing well.

How to reproduce
Test script

<?php

use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridSmtpTransport;
use Symfony\Component\Mailer\Transport;

require 'vendor/autoload.php';

$dsn = 'sendgrid+smtp://apikey:[email protected]:587';
$transport = Transport::fromDsn($dsn);
assert($transport instanceof SendgridSmtpTransport);

var_dump($transport->getStream()->getPort());

Output:

fred@home:~/projects/symfony$ php test.php 
/home/fred/projects/symfony/test.php:13:
int(465)

Possible Solution
Ideally, this should probably be parsed and injected here https://github.com/symfony/sendgrid-mailer/blob/master/Transport/SendgridTransportFactory.php#L36, so that we can pass it down to EsmtpTransport which handles the defaults (https://github.com/symfony/mailer/blob/master/Transport/Smtp/EsmtpTransport.php) but that doesn't really leave us with a lovely constructor for SendgridSmtpTransport.

@ftdysa ftdysa added the Bug label Mar 26, 2020
@xabbuh xabbuh added the Mailer label Mar 27, 2020
jpjoao added a commit to jpjoao/symfony that referenced this issue Mar 28, 2020
@jpjoao
Copy link
Contributor

jpjoao commented Mar 28, 2020

I've created a PR based on the report that adds a unit test that reproduces this issue. (it aims branch 5.0 as that's where the bug was reported but I can backport it if needed)

I initially thought we could use the same approach as for the SendgridApiTransport and override the port before returning:

return (new SendgridApiTransport($key, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port);

Digging into it I spotted this:

if (null === $tls) {
if (465 === $port) {
$tls = true;
} else {
$tls = \defined('OPENSSL_VERSION_NUMBER') && 0 === $port && 'localhost' !== $host;
}
}
if (!$tls) {
$stream->disableTls();
}
if (0 === $port) {
$port = $tls ? 465 : 25;
}
$stream->setHost($host);
$stream->setPort($port);

There is a whole validation of tls made based on the port and overriding the port could bring some unwanted behaviour. I believe we would need to move this check to a new function so we can re-trigger it once we allow the port to be overwritten.

What are the thoughts on this? How should I proceed?

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

@Stefan39
Copy link

I don't understand why this bug still not fixed until now nearly 2 years it was add it here!

The same problem i have with gmail (also ignores the port in dsn) and i couldn't use the mailer without the tricky way to change it.

@xabbuh
Copy link
Member

xabbuh commented Dec 26, 2021

The answer is as simple as that nobody contributed the needed changes yet. PR welcome

@nicolas-grekas
Copy link
Member

May I ask why one would need to change the port / tls setting when talking to gmail/sendgrid?

Also, this can already be achieved by using a regular smtp dns, eg
smtp://smtp.gmail.com:25

@ftdysa
Copy link
Author

ftdysa commented Mar 30, 2022

smtp://smtp.gmail.com:25

Hi @nicolas-grekas, it does appear that the gmail transport parses things correctly. This is specifically for the sendgrid transport. Here are the docs on which port to use: https://docs.sendgrid.com/for-developers/sending-email/integrating-with-the-smtp-api#smtp-ports.

Minimal test case to reproduce:

<?php

use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridSmtpTransport;
use Symfony\Component\Mailer\Transport;

require 'vendor/autoload.php';

$dsns = [
    'sendgrid+smtp://apikey:[email protected]:587',
    'smtp://smtp.gmail.com:465',
];

foreach ($dsns as $dsn) {
    $transport = Transport::fromDsn($dsn);
    $port = $transport->getStream()->getPort();

    $clazz = get_class($transport);

    echo sprintf("DSN: %s, Port: %d, Transport: %s\n", $dsn, $port, $clazz);
}

Output:

DSN: sendgrid+smtp://apikey:[email protected]:587, Port: 465, Transport: Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridSmtpTransport
DSN: smtp://smtp.gmail.com:465, Port: 465, Transport: Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport

Probably just a matter of setting the port here: https://github.com/symfony/sendgrid-mailer/blob/5.4/Transport/SendgridTransportFactory.php#L36-L38.

@nicolas-grekas
Copy link
Member

for sendgrid, something like this should work:
smtp://apikey:[email protected]:587

but I'd still like an answer to my previous question: why?

@ftdysa
Copy link
Author

ftdysa commented Mar 30, 2022

for sendgrid, something like this should work: smtp://apikey:[email protected]:587

This does appear to work, still seems like a gap IMO though.

but I'd still like an answer to my previous question: why?

Because this is the port that the doc I linked to recommends?

SMTP ports
For an unencrypted or a TLS connection, use port 25, 2525, or 587.

@nicolas-grekas
Copy link
Member

This argument looks very weak to me. If everything works already with the current default, we shouldn't be looking for a problem.

I think we should close this issue (and the related PRs) as invalid.

@ftdysa
Copy link
Author

ftdysa commented Mar 30, 2022

This argument looks very weak to me. If everything works already with the current default, we shouldn't be looking for a problem.

I think we should close this issue (and the related PRs) as invalid.

🤷‍♂️ . Attempting to deviate from the default here: https://symfony.com/doc/current/mailer.html#using-a-3rd-party-transport will not work as expected. I can't remember exactly why this was the case when I originally opened this issue, but the default was not an option for me at the time.

@ftdysa ftdysa closed this as completed Mar 30, 2022
@nicolas-grekas
Copy link
Member

Thanks for your understanding.

I can't remember exactly why this was the case when I originally opened this issue, but the default was not an option for me at the time.

To anyone that wants us to reconsider closing this issue, please help answer this question first.

fabpot added a commit to symfony/symfony-docs that referenced this issue Mar 25, 2023
…ailer transport DSN (dreadnip)

This PR was submitted for the 6.3 branch but it was merged into the 5.4 branch instead.

Discussion
----------

Add note about custom port being ignored by specific mailer transport DSN

See symfony/symfony#49768

Seems like there has been some confusion about this:
* symfony/symfony#46979
* symfony/symfony#44794

People start with the default they're given by the docs, e.g. `sendgrid+smtp` and then try to append `:<port>` to configure it, which doesn't work.

As far as I know, the official recommendation is to use the regular `smtp` transport instead when using custom config: symfony/symfony#36224 (comment)

Commits
-------

b39027f Add note about custom port configuration being ignored by specific mailer transport DSN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants