From ac6c519be05460f069b944e5817e9640993c7dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20V=C3=A6versted?= Date: Fri, 4 Feb 2022 09:02:47 +0100 Subject: [PATCH] [Mailer] Allow manually start()/stop() of SmtpTransport --- src/Symfony/Component/Mailer/CHANGELOG.md | 5 +++++ .../Tests/Transport/Smtp/SmtpTransportTest.php | 13 +++++++++++++ .../Mailer/Transport/Smtp/SmtpTransport.php | 9 ++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Mailer/CHANGELOG.md b/src/Symfony/Component/Mailer/CHANGELOG.md index c94fe415da52f..5ed7108624a67 100644 --- a/src/Symfony/Component/Mailer/CHANGELOG.md +++ b/src/Symfony/Component/Mailer/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.1 +--- + +* Allow manually stop() of `SmtpTransport` + 6.0 --- diff --git a/src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php index 5f8d3ba0d8180..4111cad9256ae 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php @@ -133,6 +133,19 @@ public function testWriteEncodedRecipientAndSenderAddresses() $this->assertContains("RCPT TO:\r\n", $stream->getCommands()); $this->assertContains("RCPT TO:\r\n", $stream->getCommands()); } + + public function testStop() + { + $stream = new DummyStream(); + $envelope = new Envelope(new Address('sender@example.org'), [new Address('recipient@example.org')]); + + $transport = new SmtpTransport($stream); + $transport->send(new RawMessage('Message 1'), $envelope); + $this->assertFalse($stream->isClosed()); + + $transport->stop(); + $this->assertTrue($stream->isClosed()); + } } class DummyStream extends AbstractStream diff --git a/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php b/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php index a1e84781201cf..e92712d4d0121 100644 --- a/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php +++ b/src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php @@ -257,7 +257,14 @@ private function start(): void $this->getLogger()->debug(sprintf('Email transport "%s" started', __CLASS__)); } - private function stop(): void + /** + * Manually disconnect from the SMTP server. + * + * In most cases this is not necessary since the disconnect happens automatically on termination. + * In cases of long-running scripts, this might however make sense to avoid keeping an open + * connection to the SMTP server in between sending emails. + */ + public function stop(): void { if (!$this->started) { return;