Closed
Description
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 :
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
fails.
see also in slack : https://symfony-devs.slack.com/archives/C3EQ7S3MJ/p1737557780460529