Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #31774 [Mailer] Fix the possibility to set a From header from Mes…
…sageListener (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 ------- f4254e6 [Mailer] fixed the possibility to set a From header from MessageListener
- Loading branch information
Showing
4 changed files
with
112 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters