0

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?

2 Answers2

0

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 = ‘’;
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • I tried that but no change... Still can't find the problem. I know are different instances and that is make me thing that there is a way that I am joining or something in the view method maybe? – aikidosama Nov 18 '19 at 22:46
  • Double check everything - don’t assume that your data is what you think it is; **check**. For example, check what twig is returning before trying to use it in the email. – Synchro Nov 19 '19 at 08:29
0

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.