Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 18c9ae1

Browse filesBrowse files
committed
Handle failure when sending DATA
1 parent 8f7a8d3 commit 18c9ae1
Copy full SHA for 18c9ae1

File tree

Expand file treeCollapse file tree

2 files changed

+43
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+43
-3
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
1919
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
2020
use Symfony\Component\Mime\Address;
21+
use Symfony\Component\Mime\Email;
22+
use Symfony\Component\Mime\Exception\InvalidArgumentException;
2123
use Symfony\Component\Mime\RawMessage;
2224

2325
class SmtpTransportTest extends TestCase
@@ -86,6 +88,29 @@ public function testSendDoesPingAboveThreshold()
8688
$transport->send(new RawMessage('Message 3'), $envelope);
8789
$this->assertContains("NOOP\r\n", $stream->getCommands());
8890
}
91+
92+
public function testSendInvalidMessage()
93+
{
94+
$stream = new DummyStream();
95+
96+
$transport = new SmtpTransport($stream);
97+
$transport->setPingThreshold(1);
98+
99+
$message = new Email();
100+
$message->to('recipient@example.org');
101+
$message->from('sender@example.org');
102+
$message->attachFromPath('/does_not_exists');
103+
104+
try {
105+
$transport->send($message);
106+
$this->fail('Expected Symfony\Component\Mime\Exception\InvalidArgumentException to be thrown');
107+
} catch (InvalidArgumentException $e) {
108+
$this->assertSame('Path "/does_not_exists" is not readable.', $e->getMessage());
109+
}
110+
111+
$this->assertNotContains("\r\n.\r\n", $stream->getCommands());
112+
$this->assertTrue($stream->isClosed());
113+
}
89114
}
90115

91116
class DummyStream extends AbstractStream
@@ -164,4 +189,10 @@ public function isClosed(): bool
164189
{
165190
return $this->closed;
166191
}
192+
193+
public function terminate(): void
194+
{
195+
parent::terminate();
196+
$this->closed = true;
197+
}
167198
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,19 @@ protected function doSend(SentMessage $message): void
200200
}
201201

202202
$this->executeCommand("DATA\r\n", [354]);
203-
foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
204-
$this->stream->write($chunk, false);
203+
try {
204+
foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
205+
$this->stream->write($chunk, false);
206+
}
207+
$this->stream->flush();
208+
} catch (TransportExceptionInterface $e) {
209+
throw $e;
210+
} catch (\Exception $e) {
211+
$this->stream->terminate();
212+
$this->started = false;
213+
$this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
214+
throw $e;
205215
}
206-
$this->stream->flush();
207216
$this->executeCommand("\r\n.\r\n", [250]);
208217
$message->appendDebug($this->stream->getDebug());
209218
$this->lastMessageTime = microtime(true);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.