[Mailer] Improve an exception when trying to send a RawMessage without an Envelope
This commit is contained in:
parent
b0abc10617
commit
c6d56de86d
|
@ -12,7 +12,6 @@
|
|||
namespace Symfony\Component\Mailer;
|
||||
|
||||
use Symfony\Component\Mailer\Event\MessageEvent;
|
||||
use Symfony\Component\Mailer\Exception\TransportException;
|
||||
use Symfony\Component\Mailer\Messenger\SendEmailMessage;
|
||||
use Symfony\Component\Mailer\Transport\TransportInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
|
@ -45,15 +44,7 @@ final class Mailer implements MailerInterface
|
|||
|
||||
if (null !== $this->dispatcher) {
|
||||
$message = clone $message;
|
||||
if (null !== $envelope) {
|
||||
$envelope = clone $envelope;
|
||||
} else {
|
||||
try {
|
||||
$envelope = new DelayedSmtpEnvelope($message);
|
||||
} catch (\Exception $e) {
|
||||
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
|
||||
}
|
||||
}
|
||||
$envelope = null !== $envelope ? clone $envelope : SmtpEnvelope::create($message);
|
||||
$event = new MessageEvent($message, $envelope, (string) $this->transport, true);
|
||||
$this->dispatcher->dispatch($event);
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
namespace Symfony\Component\Mailer;
|
||||
|
||||
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Mailer\Exception\LogicException;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\Mime\RawMessage;
|
||||
|
||||
|
@ -34,6 +35,10 @@ class SmtpEnvelope
|
|||
|
||||
public static function create(RawMessage $message): self
|
||||
{
|
||||
if (RawMessage::class === \get_class($message)) {
|
||||
throw new LogicException('Cannot send a RawMessage instance without an explicit Envelope.');
|
||||
}
|
||||
|
||||
return new DelayedSmtpEnvelope($message);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Mailer\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Mailer\Exception\LogicException;
|
||||
use Symfony\Component\Mailer\Mailer;
|
||||
use Symfony\Component\Mailer\Transport\TransportInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Mime\RawMessage;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class MailerTest extends TestCase
|
||||
{
|
||||
public function testSendingRawMessages()
|
||||
{
|
||||
$this->expectException(LogicException::class);
|
||||
|
||||
$transport = new Mailer($this->createMock(TransportInterface::class), $this->createMock(MessageBusInterface::class), $this->createMock(EventDispatcherInterface::class));
|
||||
$transport->send(new RawMessage('Some raw email message'));
|
||||
}
|
||||
}
|
|
@ -12,10 +12,12 @@
|
|||
namespace Symfony\Component\Mailer\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Mailer\Exception\LogicException;
|
||||
use Symfony\Component\Mailer\SmtpEnvelope;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\Mime\Header\Headers;
|
||||
use Symfony\Component\Mime\Message;
|
||||
use Symfony\Component\Mime\RawMessage;
|
||||
|
||||
class SmtpEnvelopeTest extends TestCase
|
||||
{
|
||||
|
@ -89,4 +91,11 @@ class SmtpEnvelopeTest extends TestCase
|
|||
$e = SmtpEnvelope::create(new Message($headers));
|
||||
$this->assertEquals([new Address('to@symfony.com'), new Address('cc@symfony.com'), new Address('bcc@symfony.com')], $e->getRecipients());
|
||||
}
|
||||
|
||||
public function testFromRawMessages()
|
||||
{
|
||||
$this->expectException(LogicException::class);
|
||||
|
||||
SmtpEnvelope::create(new RawMessage('Some raw email message'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
namespace Symfony\Component\Mailer\Tests\Transport;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Mailer\Exception\LogicException;
|
||||
use Symfony\Component\Mailer\SmtpEnvelope;
|
||||
use Symfony\Component\Mailer\Transport\NullTransport;
|
||||
use Symfony\Component\Mime\Address;
|
||||
|
@ -46,4 +47,12 @@ class AbstractTransportTest extends TestCase
|
|||
$transport->send($message, $envelope);
|
||||
$this->assertEqualsWithDelta(0, time() - $start, 1);
|
||||
}
|
||||
|
||||
public function testSendingRawMessages()
|
||||
{
|
||||
$this->expectException(LogicException::class);
|
||||
|
||||
$transport = new NullTransport();
|
||||
$transport->send(new RawMessage('Some raw email message'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,9 +13,7 @@ namespace Symfony\Component\Mailer\Transport;
|
|||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\Mailer\DelayedSmtpEnvelope;
|
||||
use Symfony\Component\Mailer\Event\MessageEvent;
|
||||
use Symfony\Component\Mailer\Exception\TransportException;
|
||||
use Symfony\Component\Mailer\SentMessage;
|
||||
use Symfony\Component\Mailer\SmtpEnvelope;
|
||||
use Symfony\Component\Mime\Address;
|
||||
|
@ -56,15 +54,7 @@ abstract class AbstractTransport implements TransportInterface
|
|||
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage
|
||||
{
|
||||
$message = clone $message;
|
||||
if (null !== $envelope) {
|
||||
$envelope = clone $envelope;
|
||||
} else {
|
||||
try {
|
||||
$envelope = new DelayedSmtpEnvelope($message);
|
||||
} catch (\Exception $e) {
|
||||
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
|
||||
}
|
||||
}
|
||||
$envelope = null !== $envelope ? clone $envelope : SmtpEnvelope::create($message);
|
||||
|
||||
if (null !== $this->dispatcher) {
|
||||
$event = new MessageEvent($message, $envelope, (string) $this);
|
||||
|
|
Reference in New Issue