Skip to content

Commit 0c64ddb

Browse files
committed
Handle failure when sending DATA
1 parent 8f7a8d3 commit 0c64ddb

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

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

Lines 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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function __toString(): string
159159
return $name;
160160
}
161161

162-
return sprintf('smtp://sendmail');
162+
return 'smtp://sendmail';
163163
}
164164

165165
/**
@@ -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)