dpgbh20688 2016-11-19 16:21
浏览 72
已采纳

PHPmailer的细长框架和电子邮件模板呈现问题

I am using Slim v3 php framework and have integrated PHPMailer to send mails. I don't use any template engine like Twig, but I rather use plain PHP.

My idea is to make a HTML5 template for emails in a separate file, similar to regular page templates and then pass some variables into it, render it and send it. It all works well except for one part - rendered output also has rendered header info.

This is how my code looks like, simplified of course

// Store variables in an array
$email_content = array(
  'email__name' => $_POST['name'],
  'email__from' => $_POST['from'],
  'email__message' => $_POST['message']
);

// Render email template
$template = $this->view->render($response, "email/simple_email.phtml", $email_content);

And then I send this with PHPMailer

$mail->msgHTML($template);

Problem is that on top of HTML content I get this header data, which is visible in sent email:

HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8

Is there a way to render output without this? At the moment I am using str_replace() to remove this, but I guess that there is an elegant, built in, solution to deal with this?

  • 写回答

1条回答 默认 最新

  • dragon5006 2016-11-19 16:38
    关注

    The #render() method returns a Psr\Http\Message\ResponseInterface which contains all information about the response so also the header information.

    You only want the HTML so use the #fetch() method on the view which returns only the HTML.

    $template = $this->view->fetch("email/simple_email.phtml", $email_content);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常