Skip to content

[Mailer] Remove the auth mode DSN option and support in the eSMTP transport #33237

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

Merged
merged 1 commit into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SesSmtpTransport extends EsmtpTransport
*/
public function __construct(string $username, string $password, string $region = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct(sprintf('email-smtp.%s.amazonaws.com', $region ?: 'eu-west-1'), 587, true, null, $dispatcher, $logger);
parent::__construct(sprintf('email-smtp.%s.amazonaws.com', $region ?: 'eu-west-1'), 587, true, $dispatcher, $logger);

$this->setUsername($username);
$this->setPassword($password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GmailSmtpTransport extends EsmtpTransport
{
public function __construct(string $username, string $password, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct('smtp.gmail.com', 465, true, null, $dispatcher, $logger);
parent::__construct('smtp.gmail.com', 465, true, $dispatcher, $logger);

$this->setUsername($username);
$this->setPassword($password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MandrillSmtpTransport extends EsmtpTransport
{
public function __construct(string $username, string $password, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct('smtp.mandrillapp.com', 587, true, null, $dispatcher, $logger);
parent::__construct('smtp.mandrillapp.com', 587, true, $dispatcher, $logger);

$this->setUsername($username);
$this->setPassword($password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MailgunSmtpTransport extends EsmtpTransport
{
public function __construct(string $username, string $password, string $region = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct('us' !== ($region ?: 'us') ? sprintf('smtp.%s.mailgun.org', $region) : 'smtp.mailgun.org', 465, true, null, $dispatcher, $logger);
parent::__construct('us' !== ($region ?: 'us') ? sprintf('smtp.%s.mailgun.org', $region) : 'smtp.mailgun.org', 465, true, $dispatcher, $logger);

$this->setUsername($username);
$this->setPassword($password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PostmarkSmtpTransport extends EsmtpTransport
{
public function __construct(string $id, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct('smtp.postmarkapp.com', 587, true, null, $dispatcher, $logger);
parent::__construct('smtp.postmarkapp.com', 587, true, $dispatcher, $logger);

$this->setUsername($id);
$this->setPassword($id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SendgridSmtpTransport extends EsmtpTransport
{
public function __construct(string $key, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct('smtp.sendgrid.net', 465, true, null, $dispatcher, $logger);
parent::__construct('smtp.sendgrid.net', 465, true, $dispatcher, $logger);

$this->setUsername('apikey');
$this->setPassword($key);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

* [BC BREAK] removed the `auth_mode` DSN option (it is now always determined automatically)
* STARTTLS cannot be enabled anymore (it is used automatically if TLS is disabled and the server supports STARTTLS)
* [BC BREAK] Removed the `encryption` DSN option (use `smtps` instead)
* Added support for the `smtps` protocol (does the same as using `smtp` and port `465`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,30 @@ public function createProvider(): iterable
$eventDispatcher = $this->getDispatcher();
$logger = $this->getLogger();

$transport = new EsmtpTransport('localhost', 25, false, null, $eventDispatcher, $logger);
$transport = new EsmtpTransport('localhost', 25, false, $eventDispatcher, $logger);

yield [
new Dsn('smtp', 'localhost'),
$transport,
];

$transport = new EsmtpTransport('example.com', 99, true, 'login', $eventDispatcher, $logger);
$transport = new EsmtpTransport('example.com', 99, true, $eventDispatcher, $logger);
$transport->setUsername(self::USER);
$transport->setPassword(self::PASSWORD);

yield [
new Dsn('smtps', 'example.com', self::USER, self::PASSWORD, 99, ['auth_mode' => 'login']),
new Dsn('smtps', 'example.com', self::USER, self::PASSWORD, 99),
$transport,
];

$transport = new EsmtpTransport('example.com', 465, true, null, $eventDispatcher, $logger);
$transport = new EsmtpTransport('example.com', 465, true, $eventDispatcher, $logger);

yield [
new Dsn('smtps', 'example.com'),
$transport,
];

$transport = new EsmtpTransport('example.com', 465, true, null, $eventDispatcher, $logger);
$transport = new EsmtpTransport('example.com', 465, true, $eventDispatcher, $logger);

yield [
new Dsn('smtp', 'example.com', '', '', 465),
Expand Down
39 changes: 3 additions & 36 deletions src/Symfony/Component/Mailer/Transport/Smtp/EsmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class EsmtpTransport extends SmtpTransport
private $authenticators = [];
private $username = '';
private $password = '';
private $authMode;

public function __construct(string $host = 'localhost', int $port = 0, bool $tls = null, string $authMode = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
public function __construct(string $host = 'localhost', int $port = 0, bool $tls = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
parent::__construct(null, $dispatcher, $logger);

// order is important here (roughly most secure and popular first)
$this->authenticators = [
new Auth\CramMd5Authenticator(),
new Auth\LoginAuthenticator(),
Expand All @@ -61,9 +61,6 @@ public function __construct(string $host = 'localhost', int $port = 0, bool $tls

$stream->setHost($host);
$stream->setPort($port);
if (null !== $authMode) {
$this->setAuthMode($authMode);
}
}

public function setUsername(string $username): self
Expand All @@ -90,18 +87,6 @@ public function getPassword(): string
return $this->password;
}

public function setAuthMode(string $mode): self
{
$this->authMode = $mode;

return $this;
}

public function getAuthMode(): string
{
return $this->authMode;
}

public function addAuthenticator(AuthenticatorInterface $authenticator): void
{
$this->authenticators[] = $authenticator;
Expand Down Expand Up @@ -166,7 +151,7 @@ private function handleAuth(array $modes): void
$authNames = [];
$errors = [];
$modes = array_map('strtolower', $modes);
foreach ($this->getActiveAuthenticators() as $authenticator) {
foreach ($this->authenticators as $authenticator) {
if (!\in_array(strtolower($authenticator->getAuthKeyword()), $modes, true)) {
continue;
}
Expand Down Expand Up @@ -195,22 +180,4 @@ private function handleAuth(array $modes): void

throw new TransportException($message);
}

/**
* @return AuthenticatorInterface[]
*/
private function getActiveAuthenticators(): array
{
if (!$mode = strtolower($this->authMode)) {
return $this->authenticators;
}

foreach ($this->authenticators as $authenticator) {
if (strtolower($authenticator->getAuthKeyword()) === $mode) {
return [$authenticator];
}
}

throw new TransportException(sprintf('Auth mode "%s" is invalid.', $mode));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ final class EsmtpTransportFactory extends AbstractTransportFactory
public function create(Dsn $dsn): TransportInterface
{
$tls = 'smtps' === $dsn->getScheme() ? true : null;
$authMode = $dsn->getOption('auth_mode');
$port = $dsn->getPort(0);
$host = $dsn->getHost();

$transport = new EsmtpTransport($host, $port, $tls, $authMode, $this->dispatcher, $this->logger);
$transport = new EsmtpTransport($host, $port, $tls, $this->dispatcher, $this->logger);

if ($user = $dsn->getUser()) {
$transport->setUsername($user);
Expand Down