【问题标题】:Slim framework and email template rendering issue with PHPmailerPHPmailer 的 Slim 框架和电子邮件模板呈现问题
【发布时间】:2016-11-19 16:21:09
【问题描述】:

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

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

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

// 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 内容之上,我得到了这个标题数据,它在发送的电子邮件中可见:

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

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

【问题讨论】:

    标签: php email slim psr-7


    【解决方案1】:

    #render() 方法返回一个 Psr\Http\Message\ResponseInterface,其中包含有关响应的所有信息以及标头信息。

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

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

    【讨论】:

      猜你喜欢
      • 2014-08-19
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2020-01-31
      相关资源
      最近更新 更多