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

Slim framework and email template rendering issue with PHPmailer

我正在使用 Slim v3 php 框架并集成了 PHPMailer 来发送邮件。我不使用任何像 Twig 这样的模板引擎,但我宁愿使用普通的 PHP。

我的想法是在单独的文件中为电子邮件制作一个 HTML5 模板,类似于常规页面模板,然后将一些变量传递给它,渲染它并发送它。除了一部分外,一切都很好——渲染输出也渲染了 header 信息。

这就是我的代码的样子,当然是经过简化的

// 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

发送这个
$mail->msgHTML($template);

问题是,在 HTML 内容之上,我得到了这个 header 数据,它在已发送的电子邮件中可见:

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

有没有办法在没有这个的情况下渲染输出?目前我正在使用 str_replace() 来删除它,但我想有一个优雅的内置解决方案来处理这个问题?

#render() 方法 returns 一个 Psr\Http\Message\ResponseInterface 包含有关响应的所有信息,因此也包含 header 信息。

您只想要 HTML,所以在 returns 只需要 HTML 的视图上使用 #fetch() 方法。

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