PHPmailer的苗条框架和电子邮件模板呈现问题 [英] Slim framework and email template rendering issue with PHPmailer

查看:61
本文介绍了PHPmailer的苗条框架和电子邮件模板呈现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Slim v3 php框架,并集成了PHPMailer来发送邮件.我不使用任何模板引擎,例如Twig,而是使用普通的PHP.

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.

我的想法是在一个单独的文件中为电子邮件创建HTML5模板,类似于常规页面模板,然后将一些变量传递到其中,进行渲染并发送.除了一个部分外,其他所有功能都运行良好-呈现的输出还具有呈现的标头信息.

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);

然后我用PHPMailer发送此邮件

And then I send this with PHPMailer

$mail->msgHTML($template);

问题在于,除了HTML内容之外,我还获得了此标头数据,该标头数据在已发送的电子邮件中可见:

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

有没有一种方法可以在没有这种情况的情况下呈现输出?目前,我正在使用str_replace()删除它,但是我想有一个优雅的内置解决方案可以解决这个问题?

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?

推荐答案

#render()方法返回包含所有信息的 Psr \ Http \ Message \ ResponseInterface 关于响应,还有标题信息.

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

您只需要HTML,因此在仅返回HTML的视图上使用 #fetch()方法.

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);

这篇关于PHPmailer的苗条框架和电子邮件模板呈现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆