Skip to content

Commit 3c4c608

Browse files
Merge branch '4.4'
* 4.4: [Validator] Deprecated CacheInterface in favor of PSR-6. Fix wrong namespace [Mailer] Fix typo [Mailer] Fix an error message maintain sender/recipient name in SMTP envelopes [Mailer] Improve an exception when trying to send a RawMessage without an Envelope Fix #32148 TransportException was not thrown Add ErrorController to preview and render errors
2 parents d1fb781 + 5d03f52 commit 3c4c608

24 files changed

+160
-64
lines changed

AbstractStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function write(string $bytes, bool $debug = true): void
4141
$bytesToWrite = \strlen($bytes);
4242
$totalBytesWritten = 0;
4343
while ($totalBytesWritten < $bytesToWrite) {
44-
$bytesWritten = fwrite($this->in, substr($bytes, $totalBytesWritten));
44+
$bytesWritten = @fwrite($this->in, substr($bytes, $totalBytesWritten));
4545
if (false === $bytesWritten || 0 === $bytesWritten) {
4646
throw new TransportException('Unable to write bytes on the wire.');
4747
}

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

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
}

DelayedSmtpEnvelope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getSender(): Address
4545
return parent::getSender();
4646
}
4747

48-
return new Address(self::getSenderFromHeaders($this->message->getHeaders())->getAddress());
48+
return self::getSenderFromHeaders($this->message->getHeaders());
4949
}
5050

5151
public function setRecipients(array $recipients): void
@@ -73,7 +73,7 @@ private static function getRecipientsFromHeaders(Headers $headers): array
7373
foreach (['to', 'cc', 'bcc'] as $name) {
7474
foreach ($headers->all($name) as $header) {
7575
foreach ($header->getAddresses() as $address) {
76-
$recipients[] = new Address($address->getAddress());
76+
$recipients[] = $address;
7777
}
7878
}
7979
}

Dsn.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public static function fromString(string $dsn): self
4242
}
4343

4444
if (!isset($parsedDsn['scheme'])) {
45-
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN must contain a transport scheme.', $dsn));
45+
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN must contain a scheme.', $dsn));
4646
}
4747

4848
if (!isset($parsedDsn['host'])) {
49-
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN must contain a mailer name.', $dsn));
49+
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN must contain a host (use "default" by default).', $dsn));
5050
}
5151

5252
$user = isset($parsedDsn['user']) ? urldecode($parsedDsn['user']) : null;

DsnTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ public function invalidDsnProvider(): iterable
7777

7878
yield [
7979
'//sendmail',
80-
'The "//sendmail" mailer DSN must contain a transport scheme.',
80+
'The "//sendmail" mailer DSN must contain a scheme.',
8181
];
8282

8383
yield [
8484
'file:///some/path',
85-
'The "file:///some/path" mailer DSN must contain a mailer name.',
85+
'The "file:///some/path" mailer DSN must contain a host (use "default" by default).',
8686
];
8787
}
8888
}

Exception/UnsupportedHostException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(Dsn $dsn)
5454
}
5555
$package = self::HOST_TO_PACKAGE_MAP[$provider] ?? null;
5656
if ($package && !class_exists($package['class'])) {
57-
parent::__construct(sprintf('Unable to send emails via "%s" as the bridge is not installed. Try running "composer require %s".', $host, $package['package']));
57+
parent::__construct(sprintf('Unable to send emails via "%s" as the bridge is not installed. Try running "composer require %s".', $provider, $package['package']));
5858

5959
return;
6060
}

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
}

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 <[email protected]>
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+
}

Smtp/SmtpTransport.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ protected function doSend(SentMessage $message): void
165165
{
166166
try {
167167
$envelope = $message->getEnvelope();
168-
$this->doMailFromCommand($envelope->getSender()->toString());
168+
$this->doMailFromCommand($envelope->getSender()->getAddress());
169169
foreach ($envelope->getRecipients() as $recipient) {
170-
$this->doRcptToCommand($recipient->toString());
170+
$this->doRcptToCommand($recipient->getAddress());
171171
}
172172

173173
$this->executeCommand("DATA\r\n", [354]);

0 commit comments

Comments
 (0)