Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mime] Fix undefined array key 0 when empty sender #53565

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
Jan 21, 2024

Conversation

0x346e3730
Copy link
Contributor

@0x346e3730 0x346e3730 commented Jan 17, 2024

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues N/A
License MIT

Today I managed to produce the following error :
image

From the following code :

$email = (new TemplatedEmail())
            ->from()
            ->to($member->getEmail())
            ->subject('Adhésion Sauvade')
            ->htmlTemplate('emails/new-membership/index.html.twig')
            ->context([
                'member' => $member,
            ])
            ->addPart(new DataPart(
                $this->pdfService->generatePdfFromTemplate(
                    'emails/new-membership/attachments/recu.html.twig',
                    ['member' => $member]
                ),
            ))
            ->addPart(new DataPart(
                $this->pdfService->generatePdfFromTemplate(
                    'emails/new-membership/attachments/membership-card.html.twig',
                    ['member' => $member]
                ),
            ))
        ;

        $this->mailer->send($email);

This happens because the from method from the Symfony\Component\Mime\Email class adds the header even if the array of addresses is empty. I identified two ways of fixing this :

  • Adding a check in the Symfony\Component\Mime\Message class before accessing the key 0 and throw a better exception in case the adresses are empty
  • Modifying the from method so that it doesn't add the header if the adresses are empty; or throw an exception when trying to do so.

I'm unsure what method is the cleaner, I thought that fixing the Message access of an undefined key was the widest fix (as I guess it could happen from somewhere else than the Mailer component). Also not sure how to test that and wanted to make sure this fix was good before testing it.

@xabbuh
Copy link
Member

xabbuh commented Jan 17, 2024

Can you please add a test to make sure that we don’t break this again.

@0x346e3730 0x346e3730 force-pushed the better-error-message-if-empty-from branch from 8fd3d85 to a5f39b1 Compare January 17, 2024 17:00
@0x346e3730
Copy link
Contributor Author

Done. It seems to me that the failing tests are related to cryptography errors and not my code

@@ -140,7 +140,10 @@ public function generateMessageId(): string
if ($this->headers->has('Sender')) {
$sender = $this->headers->get('Sender')->getAddress();
} elseif ($this->headers->has('From')) {
$sender = $this->headers->get('From')->getAddresses()[0];
if (empty($froms = $this->headers->get('From')->getAddresses())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (empty($froms = $this->headers->get('From')->getAddresses())) {
if ([] === $froms = $this->headers->get('From')->getAddresses()) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used the proposition of Fabien instead, letting you resolve the conversation if it's okay with you

@fabpot
Copy link
Member

fabpot commented Jan 18, 2024

What about forbidding calling Email::from() without any address instead?

@0x346e3730
Copy link
Contributor Author

What about forbidding calling Email::from() without any address instead?

This is one of the ways I identified to fix this issue, I have no opinion on which is best for DX.

I went with fixing it in the Message class because it could (I guess ?) be used by other components than Mailer so I would suggest to still fix this here even if we block this behavior in the from method

@fabpot
Copy link
Member

fabpot commented Jan 18, 2024

I would do the fix in Email::from() anyway as there is no good use case for not passing anything to this method.

@0x346e3730
Copy link
Contributor Author

I adjusted the code with your reviews and added an exception if calling Email::from() with no addres, however the from method just calls setListAddressHeaderBody that is also called by replyTo, to, cc, bcc and addListAddressHeaderBody. Should we implement the throw in Email::setListAddressHeaderBody() instead so that it covers all method that add email addresses to certain headers ?

Plus the "add" version of those methods (addFrom, addReplyTo ect...) call addListAddressHeaderBody that does $header->addAddresses(Address::createArray($addresses));. Should we do the same throw in MailboxListHeader::addAddresses as well ?

@fabpot fabpot force-pushed the better-error-message-if-empty-from branch from e94e473 to ee49a78 Compare January 21, 2024 09:54
@fabpot
Copy link
Member

fabpot commented Jan 21, 2024

Thank you @0x346e3730.

@fabpot fabpot merged commit f3cf7e0 into symfony:5.4 Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants