Handle failure when sending DATA
This commit is contained in:
parent
8f7a8d345a
commit
849211a780
@ -18,6 +18,8 @@ use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
|
|||||||
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
|
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
|
||||||
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
|
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
|
||||||
use Symfony\Component\Mime\Address;
|
use Symfony\Component\Mime\Address;
|
||||||
|
use Symfony\Component\Mime\Email;
|
||||||
|
use Symfony\Component\Mime\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\Mime\RawMessage;
|
use Symfony\Component\Mime\RawMessage;
|
||||||
|
|
||||||
class SmtpTransportTest extends TestCase
|
class SmtpTransportTest extends TestCase
|
||||||
@ -86,6 +88,29 @@ class SmtpTransportTest extends TestCase
|
|||||||
$transport->send(new RawMessage('Message 3'), $envelope);
|
$transport->send(new RawMessage('Message 3'), $envelope);
|
||||||
$this->assertContains("NOOP\r\n", $stream->getCommands());
|
$this->assertContains("NOOP\r\n", $stream->getCommands());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSendInvalidMessage()
|
||||||
|
{
|
||||||
|
$stream = new DummyStream();
|
||||||
|
|
||||||
|
$transport = new SmtpTransport($stream);
|
||||||
|
$transport->setPingThreshold(1);
|
||||||
|
|
||||||
|
$message = new Email();
|
||||||
|
$message->to('recipient@example.org');
|
||||||
|
$message->from('sender@example.org');
|
||||||
|
$message->attachFromPath('/does_not_exists');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$transport->send($message);
|
||||||
|
$this->fail('Expected Symfony\Component\Mime\Exception\InvalidArgumentException to be thrown');
|
||||||
|
} catch (InvalidArgumentException $e) {
|
||||||
|
$this->assertMatchesRegularExpression('{Path "/does_not_exists"}i', $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertNotContains("\r\n.\r\n", $stream->getCommands());
|
||||||
|
$this->assertTrue($stream->isClosed());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DummyStream extends AbstractStream
|
class DummyStream extends AbstractStream
|
||||||
@ -164,4 +189,10 @@ class DummyStream extends AbstractStream
|
|||||||
{
|
{
|
||||||
return $this->closed;
|
return $this->closed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function terminate(): void
|
||||||
|
{
|
||||||
|
parent::terminate();
|
||||||
|
$this->closed = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,7 +159,7 @@ class SmtpTransport extends AbstractTransport
|
|||||||
return $name;
|
return $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sprintf('smtp://sendmail');
|
return 'smtp://sendmail';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -200,10 +200,19 @@ class SmtpTransport extends AbstractTransport
|
|||||||
}
|
}
|
||||||
|
|
||||||
$this->executeCommand("DATA\r\n", [354]);
|
$this->executeCommand("DATA\r\n", [354]);
|
||||||
foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
|
try {
|
||||||
$this->stream->write($chunk, false);
|
foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
|
||||||
|
$this->stream->write($chunk, false);
|
||||||
|
}
|
||||||
|
$this->stream->flush();
|
||||||
|
} catch (TransportExceptionInterface $e) {
|
||||||
|
throw $e;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->stream->terminate();
|
||||||
|
$this->started = false;
|
||||||
|
$this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
$this->stream->flush();
|
|
||||||
$this->executeCommand("\r\n.\r\n", [250]);
|
$this->executeCommand("\r\n.\r\n", [250]);
|
||||||
$message->appendDebug($this->stream->getDebug());
|
$message->appendDebug($this->stream->getDebug());
|
||||||
$this->lastMessageTime = microtime(true);
|
$this->lastMessageTime = microtime(true);
|
||||||
|
Reference in New Issue
Block a user