英文:
the variable $context is not interpreted correctly in the template
问题
I can't get out of this error: Object of class Symfony\Bridge\Twig\Mime\WrappedTemplatedEmail could not be converted to string
My setup:
php 8.2
symfony 6.2
mailer 6.2
and i use maildev
My service :
namespace App\Service;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
class SendMailService
{
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function send(string $from, string $to, string $subject, string $template, array $context): void
{
$email = (new TemplatedEmail())
->from($from)
->to($to)
->subject($subject)
->htmlTemplate("emails/$template.html.twig")
->context($context);
// On envoie le mail
$this->mailer->send($email);
}
}
if I put ->context([$context])
instead of ->context($context)
, in my template I get this error: there is no variable 'prenom'
My controller action :
#[Route('/app_email', name: 'app_email', methods: ['POST'])]
public function sendEmail(Request $request, SendMailService $mail): Response
{
if ($request->isXmlHttpRequest()) {
$from = $this->getParameter('from_email');
$emailAdressTo = $request->request->get('email'); // To
$template = 'contact';
$subject = "Envoi à partir du formulaire de contact du site Vous assurance";
$nom = $request->request->get('nom');
$prenom = $request->request->get('prenom');
$entreprise = $request->request->get('entreprise');
$message = $request->request->get('message');
$context = array(
'nom' => $nom,
'prenom' => $prenom,
'entreprise' => $entreprise,
'message' => $message,
);
dump($from);
dump($emailAdressTo);
dump($subject);
dump($template);
dump(gettype($context));
$mail->send($from, $emailAdressTo, $subject, $template, $context);
return new Response("ok");
}
return new Response("Erreur : ceci n'est pas une requête ajax", 400);
}
Dump result :
Dumped Contents In MainController.php line 54: "xxxx@xxx.fr" In MainController.php line 55: "xxxxx@xxxxx.com" In MainController.php line 56: "Envoi à partir du formulaire de contact du site" In MainController.php line 57: "contact" In MainController.php line 58: "array"
if I comment out the $context variable, my email is sent
My ajax function :
var formNewContact = $('form[id="formContact"]');
formNewContact.on("submit", function (e) {
e.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
method: "POST",
url: "{{ path('app_email') }}",
data: formData,
processData: false,
contentType: false,
success: function (response) {
if (response === 'ok') {
} else {
alert("Erreur");
}
}, // Fin success
error: function () {
alert("Erreur serveur");
}, // Fin error
}); // Fin ajax
}); // Fin function submit form contact
My template :
<p>Bonjour,</p>
<p><strong>De :</strong>{{ prenom }}<strong>{{ nom }}.</strong></p>
<p><strong>Email : </strong>{{ email }}</p>
<p><strong>Entreprise : </strong>{{ entreprise }}</p>
<p><strong>Message :</strong></p>
<hr />
<p>{{ message }}</p>
<hr />
</div>
dump(gettype($context)); -> array
Thanks for your help.
I changed :
In form this field :
<div class="mb-3">
<label for="emailTo" class="form-label">Mail (obligatoire)</label>
<input type="email" class="form-control" name="emailTo" id="emailTo" aria-describedby="mailHelp" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required>
<div class="valid-feedback feedback-pos">
Adresse email OK
</div>
<div class="invalid-feedback feedback-pos">
Cette adresse email n'est pas correcte
</div>
</div>
英文:
I can't get out of this error: Object of class Symfony\Bridge\Twig\Mime\WrappedTemplatedEmail could not be converted to string
My setup :
php 8.2
symfony 6.2
mailer 6.2
and i use maildev
I would like to generate a mail sending with ajax.
My service :
<?php
namespace App\Service;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
class SendMailService
{
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function send(string $from, string $to, string $subject, string $template, array $context): void
{
$email = (new TemplatedEmail())
->from($from)
->to($to)
->subject($subject)
->htmlTemplate("emails/$template.html.twig")
->context($context);
// On envoie le mail
$this->mailer->send($email);
}
}
if I put ->context([$context]) instead of ->context($context), in my template I get this error: there is no variable 'prenom'
My controller action :
#[Route('/app_email', name: 'app_email', methods: ['POST'])]
public function sendEmail(Request $request, SendMailService $mail): Response
{
if ($request->isXmlHttpRequest()) {
$from = $this->getParameter('from_email');
$emailAdressTo = $request->request->get('email'); // To
$template = 'contact';
$subject = "Envoi à partir du formulaire de contact du site Vous assurance";
$nom = $request->request->get('nom');
$prenom = $request->request->get('prenom');
$entreprise = $request->request->get('entreprise');
$message = $request->request->get('message');
$context = array(
'nom' => $nom,
'prenom' => $prenom,
'entreprise' => $entreprise,
'message' => $message,
);
dump($from);
dump($emailAdressTo);
dump($subject);
dump($template);
dump(gettype($context));
$mail->send($from, $emailAdressTo, $subject, $template, $context);
return new Response("ok");
}
return new Response("Erreur : ceci n'est pas une requête ajax", 400);
}
Dump result :
Dumped Contents In MainController.php line 54: "xxxx@xxx.fr" In MainController.php line 55: "xxxxx@xxxxx.com" In MainController.php line 56: "Envoi à partir du formulaire de contact du site" In MainController.php line 57: "contact" In MainController.php line 58: "array"
if I comment out the $context variable, my email is sent
My ajax function :
var formNewContact = $('form[id="formContact"]');
formNewContact.on("submit", function (e) {
e.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
method: "POST",
url: "{{ path('app_email') }}",
data: formData,
processData: false,
contentType: false,
success: function (response) {
if (response === 'ok') {
} else {
alert("Erreur");
}
}, // Fin success
error: function () {
alert("Erreur serveur");
}, // Fin error
}); // Fin ajax
}); // Fin function submit form contact
My template :
<p>Bonjour,</p>
<p><strong>De : </strong>{{ prenom }}<strong>{{ nom }}</strong>.</p>
<p><strong>Email : </strong>{{ email }}</p>
<p><strong>Entreprise : </strong>{{ entreprise }}</p>
<p><strong>Message :</strong></p>
<hr />
<p>{{message}}</p>
<hr />
</div>
dump(gettype($context)); -> array
Thanks for your help.
I changed :
In form this field :
<div class="mb-3">
<label for="emailTo" class="form-label">Mail (obligatoire)</label>
<input type="email" class="form-control" name="emailTo" id="emailTo" aria-describedby="mailHelp" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required>
<div class="valid-feedback feedback-pos">
Adresse email OK
</div>
<div class="invalid-feedback feedback-pos">
Cette adresse email n'est pas correcte
</div>
</div>
答案1
得分: 1
你的模板中,你尝试将电子邮件实例渲染为字符串,使用 {{ email }}
。这就是为什么你会收到这个错误消息。
也许你想要打印电子邮件地址:{{ email.to[0].address }}
查看:https://symfony.com/doc/current/mailer.html#html-content
Twig 模板可以访问传递给 TemplatedEmail 类的 context() 方法的任何参数,还可以访问一个名为 email 的特殊变量,它是 WrappedTemplatedEmail 的一个实例。
英文:
In your template, you are trying to render the email instance as a string with {{ email }}
. That's why you get this error.
Maybe you want to print the email address instead : {{ email.to[0].address }}
See : https://symfony.com/doc/current/mailer.html#html-content
> The Twig template has access to any of the parameters passed in the context() method of the TemplatedEmail class and also to a special variable called email, which is an instance of WrappedTemplatedEmail.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论