Skip to content

[Mailer] Improve an exception when trying to send a RawMessage without an Envelope #33454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/Symfony/Component/Mailer/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,15 +44,7 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): void

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);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/SmtpEnvelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

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);
}

Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/Mailer/Tests/MailerTest.php
Original file line number Diff line number Diff line change
@@ -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'));
}
}
9 changes: 9 additions & 0 deletions src/Symfony/Component/Mailer/Tests/SmtpEnvelopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -89,4 +91,11 @@ public function testRecipientsFromHeaders()
$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'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,4 +47,12 @@ public function testThrottling()
$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'));
}
}
12 changes: 1 addition & 11 deletions src/Symfony/Component/Mailer/Transport/AbstractTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

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;
Expand Down Expand Up @@ -56,15 +54,7 @@ public function setMaxPerSecond(float $rate): self
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);
Expand Down