bug #31774 [Mailer] Fix the possibility to set a From header from MessageListener (fabpot)
This PR was merged into the 4.3 branch.
Discussion
----------
[Mailer] Fix the possibility to set a From header from MessageListener
| Q | A
| ------------- | ---
| Branch? | 4.3
| Bug fix? | yes
| New feature? | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks? | no <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass? | yes <!-- please add some, will be required by reviewers -->
| Fixed tickets | #31733
| License | MIT
| Doc PR | n/a
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.
Additionally (see https://symfony.com/roadmap):
- Bug fixes must be submitted against the lowest maintained branch where they apply
(lowest branches are regularly merged to upper ones so they get the fixes too).
- Features and deprecations must be submitted against branch 4.4.
- Legacy code removals go to the master branch.
-->
Commits
-------
f4254e6f5e
[Mailer] fixed the possibility to set a From header from MessageListener
This commit is contained in:
commit
2438b1422a
105
src/Symfony/Component/Mailer/DelayedSmtpEnvelope.php
Normal file
105
src/Symfony/Component/Mailer/DelayedSmtpEnvelope.php
Normal file
@ -0,0 +1,105 @@
|
||||
<?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;
|
||||
|
||||
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Mailer\Exception\LogicException;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\Mime\Header\Headers;
|
||||
use Symfony\Component\Mime\Message;
|
||||
use Symfony\Component\Mime\RawMessage;
|
||||
|
||||
/**
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @experimental in 4.3
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class DelayedSmtpEnvelope extends SmtpEnvelope
|
||||
{
|
||||
private $senderSet = false;
|
||||
private $recipientsSet = false;
|
||||
private $message;
|
||||
|
||||
public function __construct(RawMessage $message)
|
||||
{
|
||||
if (!$message instanceof Message) {
|
||||
// FIXME: parse the raw message to create the envelope?
|
||||
throw new InvalidArgumentException(sprintf('Unable to create an SmtpEnvelope from a "%s" message.', RawMessage::class));
|
||||
}
|
||||
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
public function setSender(Address $sender): void
|
||||
{
|
||||
parent::setSender($sender);
|
||||
|
||||
$this->senderSet = true;
|
||||
}
|
||||
|
||||
public function getSender(): Address
|
||||
{
|
||||
if ($this->senderSet) {
|
||||
return parent::getSender();
|
||||
}
|
||||
|
||||
return self::getSenderFromHeaders($this->message->getHeaders());
|
||||
}
|
||||
|
||||
public function setRecipients(array $recipients): void
|
||||
{
|
||||
parent::setRecipients($recipients);
|
||||
|
||||
$this->recipientsSet = parent::getRecipients();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Address[]
|
||||
*/
|
||||
public function getRecipients(): array
|
||||
{
|
||||
if ($this->recipientsSet) {
|
||||
return parent::getRecipients();
|
||||
}
|
||||
|
||||
return self::getRecipientsFromHeaders($this->message->getHeaders());
|
||||
}
|
||||
|
||||
private static function getRecipientsFromHeaders(Headers $headers): array
|
||||
{
|
||||
$recipients = [];
|
||||
foreach (['to', 'cc', 'bcc'] as $name) {
|
||||
foreach ($headers->getAll($name) as $header) {
|
||||
$recipients = array_merge($recipients, $header->getAddresses());
|
||||
}
|
||||
}
|
||||
|
||||
return $recipients;
|
||||
}
|
||||
|
||||
private static function getSenderFromHeaders(Headers $headers): Address
|
||||
{
|
||||
if ($return = $headers->get('Return-Path')) {
|
||||
return $return->getAddress();
|
||||
}
|
||||
if ($sender = $headers->get('Sender')) {
|
||||
return $sender->getAddress();
|
||||
}
|
||||
if ($from = $headers->get('From')) {
|
||||
return $from->getAddresses()[0];
|
||||
}
|
||||
|
||||
throw new LogicException('Unable to determine the sender of the message.');
|
||||
}
|
||||
}
|
@ -12,10 +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\Header\Headers;
|
||||
use Symfony\Component\Mime\Message;
|
||||
use Symfony\Component\Mime\NamedAddress;
|
||||
use Symfony\Component\Mime\RawMessage;
|
||||
|
||||
@ -40,14 +37,7 @@ class SmtpEnvelope
|
||||
|
||||
public static function create(RawMessage $message): self
|
||||
{
|
||||
if ($message instanceof Message) {
|
||||
$headers = $message->getHeaders();
|
||||
|
||||
return new self(self::getSenderFromHeaders($headers), self::getRecipientsFromHeaders($headers));
|
||||
}
|
||||
|
||||
// FIXME: parse the raw message to create the envelope?
|
||||
throw new InvalidArgumentException(sprintf('Unable to create an SmtpEnvelope from a "%s" message.', RawMessage::class));
|
||||
return new DelayedSmtpEnvelope($message);
|
||||
}
|
||||
|
||||
public function setSender(Address $sender): void
|
||||
@ -84,31 +74,4 @@ class SmtpEnvelope
|
||||
{
|
||||
return $this->recipients;
|
||||
}
|
||||
|
||||
private static function getRecipientsFromHeaders(Headers $headers): array
|
||||
{
|
||||
$recipients = [];
|
||||
foreach (['to', 'cc', 'bcc'] as $name) {
|
||||
foreach ($headers->getAll($name) as $header) {
|
||||
$recipients = array_merge($recipients, $header->getAddresses());
|
||||
}
|
||||
}
|
||||
|
||||
return $recipients;
|
||||
}
|
||||
|
||||
private static function getSenderFromHeaders(Headers $headers): Address
|
||||
{
|
||||
if ($return = $headers->get('Return-Path')) {
|
||||
return $return->getAddress();
|
||||
}
|
||||
if ($sender = $headers->get('Sender')) {
|
||||
return $sender->getAddress();
|
||||
}
|
||||
if ($from = $headers->get('From')) {
|
||||
return $from->getAddresses()[0];
|
||||
}
|
||||
|
||||
throw new LogicException('Unable to determine the sender of the message.');
|
||||
}
|
||||
}
|
||||
|
@ -72,12 +72,13 @@ class SmtpEnvelopeTest extends TestCase
|
||||
$this->assertEquals('from@symfony.com', $e->getSender()->getAddress());
|
||||
}
|
||||
|
||||
public function testSenderFromHeadersWithoutData()
|
||||
public function testSenderFromHeadersWithoutFrom()
|
||||
{
|
||||
$this->expectException(\LogicException::class);
|
||||
$headers = new Headers();
|
||||
$headers->addMailboxListHeader('To', ['from@symfony.com']);
|
||||
SmtpEnvelope::create(new Message($headers));
|
||||
$e = SmtpEnvelope::create($message = new Message($headers));
|
||||
$message->getHeaders()->addMailboxListHeader('From', ['from@symfony.com']);
|
||||
$this->assertEquals('from@symfony.com', $e->getSender()->getAddress());
|
||||
}
|
||||
|
||||
public function testRecipientsFromHeaders()
|
||||
|
@ -15,6 +15,7 @@ use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Mailer\DelayedSmtpEnvelope;
|
||||
use Symfony\Component\Mailer\Event\MessageEvent;
|
||||
use Symfony\Component\Mailer\Exception\TransportException;
|
||||
use Symfony\Component\Mailer\SentMessage;
|
||||
@ -62,7 +63,7 @@ abstract class AbstractTransport implements TransportInterface
|
||||
$envelope = clone $envelope;
|
||||
} else {
|
||||
try {
|
||||
$envelope = SmtpEnvelope::create($message);
|
||||
$envelope = new DelayedSmtpEnvelope($message);
|
||||
} catch (\Exception $e) {
|
||||
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
|
||||
}
|
||||
|
Reference in New Issue
Block a user