Is there a way to unset or delete msgHTML/view template in phpmailer

喜你入骨 提交于 2020-01-16 18:22:14

问题


I have a website with an email form response. I need to send 2 emails when clicked and is working with slim framework, msgHTML and twig. I set twig template by a condition to show one part of that template for the first email and other part for the another. Don't work because the first email body message was added to the second one.

So I created 2 instances of phpmailer to send 2 separate twig templates but the same error appears, it add the 2 templates in the last email body message.

Is there a way to unset the msgHTML message or the view?

$mailu = new PHPMailer; $maila = new PHPMailer;
$mailu->isHTML(true); $maila->isHTML(true);
$mailu->isSMTP(); $maila->isSMTP();
$mailu->Host = 'XXX'; $maila->Host = 'XXX';
$mailu->Port = 587; $maila->Port = 587;
$mailu->SMTPAuth = true; $maila->SMTPAuth = true;
$mailu->SMTPSecure = 'tls'; $maila->SMTPSecure = 'tls';
$mailu->Username = 'XXX; $maila->Username = 'XXX';
$mailu->Password = 'XXX'; $maila->Password = 'XXX';
$mailu->SMTPDebug = 0; $maila->SMTPDebug = 0;
$mailu->setLanguage('es'); $maila->setLanguage('es');
$mailu->CharSet="utf-8"; $maila->CharSet="utf-8";
$mailu->clearCustomHeaders(); $maila->clearCustomHeaders();
$mailu->Subject = $subject; $maila->Subject = $subject;

$marrayu = array('user'=>true, 'name'=>$name, 'email'=>$email, 'subject'=>$subject, 'message'=>$message, 'phone'=>$phone);
$mailu->msgHTML($this->view->render($response, 'templates/mailu.twig', array( "marray" => $marrayu )));
$mailu->setFrom($domainmail, 'NMV');
$mailu->addAddress($email, $name);
$mailu->addReplyTo($domainmail, 'NMV');
$mailu->send();

$marraya = array('user'=>false, 'name'=>$name, 'email'=>$email, 'subject'=>$subject, 'message'=>$message, 'phone'=>$phone);
$maila->msgHTML($this->view->render($response, 'templates/maila.twig', array( "marray" => $marraya )));
$maila->setFrom($email, $name);
$maila->addAddress($domainmail,'NMV');
$maila->send();

return $this->response->withStatus(200)->withHeader('Location',  $anchor);

Any suggestions?


回答1:


msgHTML does overwrite the Body property, and a new instance has no way of retaining the body from another instance, so I would look further upstream for the cause of that.

You can however clear any existing body like this:

$mail->Body = ‘’;
$mail->AltBody = ‘’;



回答2:


I found the answer in this post Slim framework and email template rendering issue with PHPmailer

As I expect, all was fine but the view was assuming the render to join, so change it to fetch made the trick also because I was getting the HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 header in the top of the emails.

Thanks for the help.



来源:https://stackoverflow.com/questions/58886632/is-there-a-way-to-unset-or-delete-msghtml-view-template-in-phpmailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!