8000 sending a mime email using `setBody` fails `ensureBodyValid` check on send · Issue #59582 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

sending a mime email using setBody fails ensureBodyValid check on send #59582

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

Closed
peterpeppered opened this issue Jan 22, 2025 · 1 comment
Closed

Comments

@peterpeppered
Copy link
peterpeppered commented Jan 22, 2025

Symfony version(s) affected

7.2.1

Description

Using the Email class to send mail without using the text() and html() methods, but setting the body manually with setBody() triggers a validation error on send. the ensureBodyValid method checks for the existence of the html and text properties, but fails to see that setBody added the needed parts.

How to reproduce

<?php

require_once __DIR__ . '/vendor/autoload_runtime.php';

use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\TextPart;

$textContent = new TextPart('Lorem ipsum...');
$htmlContent = new TextPart('<h1>Lorem ipsum</h1> <p>...</p>', 'utf-8', 'html', 'quoted-printable');
$body = new AlternativePart($textContent, $htmlContent);

$email = new Email();
$email
    ->from('test@example.com')
    ->to('peter@example.com')
    ->setBody($body);

dd($email->ensureValidity());

Possible Solution

No response

Additional Context

doing this :

        $textContent = new TextPart('Lorem ipsum...');
        $htmlContent = new TextPart('<h1>Lorem ipsum</h1> <p>...</p>', 'utf-8', 'html', 'quoted-printable');
        $body = new AlternativePart($textContent, $htmlContent);

        $email = new Email();
        $email
            ->from('test@example.com')
            ->to('peter@example.com')
            ->setBody($body);

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

triggers an exception :

Image

which is unexpected. as a workaround, faking the html and text properties on the Email instance makes it work :

        $textContent = new TextPart('Lorem ipsum...');
        $htmlContent = new TextPart('<h1>Lorem ipsum</h1> <p>...</p>', 'utf-8', 'html', 'quoted-printable');
        $body = new AlternativePart($textContent, $htmlContent);

        $email = new Email();
        $email
            ->from('test@peppered.com')
            ->to('peter@peppered.com')
            ->text('')
            ->html('')
            ->setBody($body);

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

I suppose this is because setBody is inherited from the Message class, and hence does not set the text and html properties on the Email, so this check

Image

fails.

see also in slack : https://symfony-devs.slack.com/archives/C3EQ7S3MJ/p1737557780460529

@stof
Copy link
Member
stof commented Jan 22, 2025

given that getBody supports getting it from the parent implementation instead of always generating it from text and html, we indeed need to account for that case in the validation.

Note that the right fix is not to override setBody. The fix should be that ensureBodyValidation should also check parent::getBody() === null in its condition.

@xabbuh xabbuh added the Mailer label Jan 22, 2025
fabpot added a commit that referenced this issue Jan 25, 2025
…age::setBody()` (alexandre-daubois)

This PR was merged into the 6.4 branch.

Discussion
----------

[Mime] Fix body validity check in `Email` when using `Message::setBody()`

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #59582
| License       | MIT

Commits
-------

b983d1b [Mime] Fix body validity check in `Email` when using `Message::setBody()`
@fabpot fabpot closed this as completed Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
0