Skip to content

Commit cbef9dd

Browse files
committed
-
1 parent 93e88b6 commit cbef9dd

File tree

7 files changed

+70
-14
lines changed

7 files changed

+70
-14
lines changed

src/Symfony/Component/Mailer/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ CHANGELOG
44
4.4.0
55
-----
66

7-
* Removed the `encryption` DSN option (use `smtps` instead)
8-
* Added support for the `stmps` protocol (does the same as using `smtp` and port `465`)
7+
* STARTTLS cannot be enabled anymore (it is used automatically if TLS is disabled and the server supports STARTTLS)
8+
* [BC BREAK] Removed the `encryption` DSN option (use `smtps` instead)
9+
* Added support for the `smtps` protocol (does the same as using `smtp` and port `465`)
910
* Added PHPUnit constraints
1011
* Added `MessageDataCollector`
1112
* Added `MessageEvents` and `MessageLoggerListener` to allow collecting sent emails
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests\Transport\Smtp;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
16+
17+
class SmtpTransportTest extends TestCase
18+
{
19+
public function testName()
20+
{
21+
$t = new EsmtpTransport();
22+
$this->assertEquals('smtp://localhost', $t->getName());
23+
24+
$t = new EsmtpTransport('example.com');
25+
$this->assertEquals('smtps://example.com', $t->getName());
26+
27+
$t = new EsmtpTransport('example.com', 2525);
28+
$this->assertEquals('smtp://example.com:2525', $t->getName());
29+
30+
$t = new EsmtpTransport('example.com', 0, true);
31+
$this->assertEquals('smtps://example.com', $t->getName());
32+
33+
$t = new EsmtpTransport('example.com', 0, false);
34+
$this->assertEquals('smtp://example.com', $t->getName());
35+
36+
$t = new EsmtpTransport('example.com', 466, true);
37+
$this->assertEquals('smtps://example.com:466', $t->getName());
38+
}
39+
}

src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class SmtpTransportTest extends TestCase
2020
public function testName()
2121
{
2222
$t = new SmtpTransport();
23-
$this->assertEquals('smtp://localhost:25', $t->getName());
23+
$this->assertEquals('smtps://localhost', $t->getName());
2424

25-
$t = new SmtpTransport((new SocketStream())->setHost('127.0.0.1')->setPort(2525));
25+
$t = new SmtpTransport((new SocketStream())->setHost('127.0.0.1')->setPort(2525)->disableTls());
2626
$this->assertEquals('smtp://127.0.0.1:2525', $t->getName());
2727
}
2828
}

src/Symfony/Component/Mailer/Transport/Smtp/EsmtpTransport.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class EsmtpTransport extends SmtpTransport
3131
private $password = '';
3232
private $authMode;
3333

34-
public function __construct(string $host = 'localhost', int $port = 25, bool $tls = true, string $authMode = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
34+
public function __construct(string $host = 'localhost', int $port = 0, bool $tls = null, string $authMode = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
3535
{
3636
parent::__construct(null, $dispatcher, $logger);
3737

@@ -44,11 +44,19 @@ public function __construct(string $host = 'localhost', int $port = 25, bool $tl
4444

4545
/** @var SocketStream $stream */
4646
$stream = $this->getStream();
47-
$stream->setHost($host);
48-
$stream->setPort($port);
47+
48+
if (null === $tls) {
49+
$tls = defined('OPENSSL_VERSION_NUMBER') && (0 === $port || 465 === $port) && 'localhost' !== $host;
50+
}
4951
if (!$tls) {
5052
$stream->disableTls();
5153
}
54+
if (0 === $port) {
55+
$port = $tls ? 465 : 25;
56+
}
57+
58+
$stream->setHost($host);
59+
$stream->setPort($port);
5260
if (null !== $authMode) {
5361
$this->setAuthMode($authMode);
5462
}
@@ -109,7 +117,7 @@ protected function doHeloCommand(): void
109117

110118
/** @var SocketStream $stream */
111119
$stream = $this->getStream();
112-
if (!$stream->isTLS() && \array_key_exists('STARTTLS', $capabilities)) {
120+
if (!$stream->isTLS() && defined('OPENSSL_VERSION_NUMBER') && \array_key_exists('STARTTLS', $capabilities)) {
113121
$this->executeCommand("STARTTLS\r\n", [220]);
114122

115123
if (!$stream->startTLS()) {

src/Symfony/Component/Mailer/Transport/Smtp/EsmtpTransportFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ final class EsmtpTransportFactory extends AbstractTransportFactory
2222
{
2323
public function create(Dsn $dsn): TransportInterface
2424
{
25-
$tls = 'smtps' === $dsn->getScheme() || 465 === $dsn->getPort(25);
25+
$tls = 'smtps' === $dsn->getScheme() ? true : null;
2626
$authMode = $dsn->getOption('auth_mode');
27-
$port = $dsn->getPort($tls ? 465 : 25);
27+
$port = $dsn->getPort(0);
2828
$host = $dsn->getHost();
2929

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

src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
129129
public function getName(): string
130130
{
131131
if ($this->stream instanceof SocketStream) {
132-
return sprintf('smtp://%s:%d', $this->stream->getHost(), $this->stream->getPort());
132+
$name = sprintf('smtp%s://%s', ($tls = $this->stream->isTLS()) ? 's' : '', $this->stream->getHost());
133+
$port = $this->stream->getPort();
134+
if (!(25 === $port || ($tls && 465 === $port))) {
135+
$name .= ':'.$port;
136+
}
137+
138+
return $name;
133139
}
134140

135141
return sprintf('smtp://sendmail');

src/Symfony/Component/Mailer/Transport/Smtp/Stream/SocketStream.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class SocketStream extends AbstractStream
2525
{
2626
private $url;
2727
private $host = 'localhost';
28-
private $port = 25;
28+
private $port = 465;
2929
private $timeout = 15;
3030
private $tls = true;
3131
private $sourceIp;
@@ -71,11 +71,13 @@ public function getPort(): int
7171
}
7272

7373
/**
74-
* Sets the TLS/SSL on the socket (disables STARTTLS is available).
74+
* Sets the TLS/SSL on the socket (disables STARTTLS).
7575
*/
76-
public function disableTls()
76+
public function disableTls(): self
7777
{
7878
$this->tls = false;
79+
80+
return $this;
7981
}
8082

8183
public function isTLS(): bool

0 commit comments

Comments
 (0)