Skip to content

Commit 0879c48

Browse files
committed
[Mailer] Improve an exception when trying to send a RawMessage without an Envelope
1 parent 610a4e9 commit 0879c48

File tree

6 files changed

+56
-21
lines changed

6 files changed

+56
-21
lines changed

src/Symfony/Component/Mailer/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, $this->transport->getName(), true);
5849
$this->dispatcher->dispatch($event);
5950
}

src/Symfony/Component/Mailer/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

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+
}

src/Symfony/Component/Mailer/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
}

src/Symfony/Component/Mailer/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
}

src/Symfony/Component/Mailer/Transport/AbstractTransport.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use Psr\Log\LoggerInterface;
1515
use Psr\Log\NullLogger;
1616
use Symfony\Component\EventDispatcher\EventDispatcher;
17-
use Symfony\Component\Mailer\DelayedSmtpEnvelope;
1817
use Symfony\Component\Mailer\Event\MessageEvent;
19-
use Symfony\Component\Mailer\Exception\TransportException;
2018
use Symfony\Component\Mailer\SentMessage;
2119
use Symfony\Component\Mailer\SmtpEnvelope;
2220
use Symfony\Component\Mime\Address;
@@ -57,15 +55,7 @@ public function setMaxPerSecond(float $rate): self
5755
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage
5856
{
5957
$message = clone $message;
60-
if (null !== $envelope) {
61-
$envelope = clone $envelope;
62-
} else {
63-
try {
64-
$envelope = new DelayedSmtpEnvelope($message);
65-
} catch (\Exception $e) {
66-
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
67-
}
68-
}
58+
$envelope = null !== $envelope ? clone $envelope : SmtpEnvelope::create($message);
6959

7060
$event = new MessageEvent($message, $envelope, $this->getName());
7161
$this->dispatcher->dispatch($event);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy