Skip to content

Commit 4493108

Browse files
committed
[Mailer] Improve an exception when trying to send a RawMessage without an Envelope
1 parent af24fa2 commit 4493108

File tree

6 files changed

+56
-21
lines changed

6 files changed

+56
-21
lines changed

Mailer.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Mailer;
1313

1414
use Symfony\Component\Mailer\Event\MessageEvent;
15-
use Symfony\Component\Mailer\Exception\TransportException;
1615
use Symfony\Component\Mailer\Messenger\SendEmailMessage;
1716
use Symfony\Component\Mailer\Transport\TransportInterface;
1817
use Symfony\Component\Messenger\MessageBusInterface;
@@ -45,15 +44,7 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): void
4544

4645
if (null !== $this->dispatcher) {
4746
$message = clone $message;
48-
if (null !== $envelope) {
49-
$envelope = clone $envelope;
50-
} else {
51-
try {
52-
$envelope = new DelayedSmtpEnvelope($message);
53-
} catch (\Exception $e) {
54-
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
55-
}
56-
}
47+
$envelope = null !== $envelope ? clone $envelope : SmtpEnvelope::create($message);
5748
$event = new MessageEvent($message, $envelope, (string) $this->transport, true);
5849
$this->dispatcher->dispatch($event);
5950
}

SmtpEnvelope.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Mailer;
1313

1414
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
15+
use Symfony\Component\Mailer\Exception\LogicException;
1516
use Symfony\Component\Mime\Address;
1617
use Symfony\Component\Mime\RawMessage;
1718

@@ -34,6 +35,10 @@ public function __construct(Address $sender, array $recipients)
3435

3536
public static function create(RawMessage $message): self
3637
{
38+
if (RawMessage::class === \get_class($message)) {
39+
throw new LogicException('Cannot send a RawMessage instance without an explicit Envelope.');
40+
}
41+
3742
return new DelayedSmtpEnvelope($message);
3843
}
3944

Tests/MailerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Exception\LogicException;
16+
use Symfony\Component\Mailer\Mailer;
17+
use Symfony\Component\Mailer\Transport\TransportInterface;
18+
use Symfony\Component\Messenger\MessageBusInterface;
19+
use Symfony\Component\Mime\RawMessage;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
22+
class MailerTest extends TestCase
23+
{
24+
public function testSendingRawMessages()
25+
{
26+
$this->expectException(LogicException::class);
27+
28+
$transport = new Mailer($this->createMock(TransportInterface::class), $this->createMock(MessageBusInterface::class), $this->createMock(EventDispatcherInterface::class));
29+
$transport->send(new RawMessage('Some raw email message'));
30+
}
31+
}

Tests/SmtpEnvelopeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
namespace Symfony\Component\Mailer\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Exception\LogicException;
1516
use Symfony\Component\Mailer\SmtpEnvelope;
1617
use Symfony\Component\Mime\Address;
1718
use Symfony\Component\Mime\Header\Headers;
1819
use Symfony\Component\Mime\Message;
20+
use Symfony\Component\Mime\RawMessage;
1921

2022
class SmtpEnvelopeTest extends TestCase
2123
{
@@ -89,4 +91,11 @@ public function testRecipientsFromHeaders()
8991
$e = SmtpEnvelope::create(new Message($headers));
9092
$this->assertEquals([new Address('to@symfony.com'), new Address('cc@symfony.com'), new Address('bcc@symfony.com')], $e->getRecipients());
9193
}
94+
95+
public function testFromRawMessages()
96+
{
97+
$this->expectException(LogicException::class);
98+
99+
SmtpEnvelope::create(new RawMessage('Some raw email message'));
100+
}
92101
}

Tests/Transport/AbstractTransportTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Mailer\Tests\Transport;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Exception\LogicException;
1516
use Symfony\Component\Mailer\SmtpEnvelope;
1617
use Symfony\Component\Mailer\Transport\NullTransport;
1718
use Symfony\Component\Mime\Address;
@@ -46,4 +47,12 @@ public function testThrottling()
4647
$transport->send($message, $envelope);
4748
$this->assertEqualsWithDelta(0, time() - $start, 1);
4849
}
50+
51+
public function testSendingRawMessages()
52+
{
53+
$this->expectException(LogicException::class);
54+
55+
$transport = new NullTransport();
56+
$transport->send(new RawMessage('Some raw email message'));
57+
}
4958
}

Transport/AbstractTransport.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Psr\Log\NullLogger;
16-
use Symfony\Component\Mailer\DelayedSmtpEnvelope;
1716
use Symfony\Component\Mailer\Event\MessageEvent;
18-
use Symfony\Component\Mailer\Exception\TransportException;
1917
use Symfony\Component\Mailer\SentMessage;
2018
use Symfony\Component\Mailer\SmtpEnvelope;
2119
use Symfony\Component\Mime\Address;
@@ -56,15 +54,7 @@ public function setMaxPerSecond(float $rate): self
5654
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage
5755
{
5856
$message = clone $message;
59-
if (null !== $envelope) {
60-
$envelope = clone $envelope;
61-
} else {
62-
try {
63-
$envelope = new DelayedSmtpEnvelope($message);
64-
} catch (\Exception $e) {
65-
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
66-
}
67-
}
57+
$envelope = null !== $envelope ? clone $envelope : SmtpEnvelope::create($message);
6858

6959
if (null !== $this->dispatcher) {
7060
$event = new MessageEvent($message, $envelope, (string) $this);

0 commit comments

Comments
 (0)