-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Mailer] Fix the possibility to set a From header from MessageListener #31774
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Mailer] fixed the possibility to set a From header from MessageListener
- Loading branch information
commit f4254e6f5e74406ca7cbcd1606d0dd3c2032d0c7
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -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 @@ public function __construct(Address $sender, array $recipients) | |
|
||
public static function create(RawMessage $message): self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we should keep this method. |
||
{ | ||
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 @@ public function getRecipients(): array | |
{ | ||
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.'); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be better to change the type-hint instead? If we want to support any other message in the future we can still change the type then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. I've done it that way to get a "better" error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it means that the user will only get an error at runtime if they didn't discover before that
Message
instances are required here. With a proper type hint they can be warned before.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would they be warned before? I suppose you're thinking about static analysis. Not sure what to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In any case, that's independent from this PR as I'm doing the same at several other places (and this code is copy/pasted from SmtpEnvelope).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabbuh see #31837