PHPMailer 在正文中发送模板失败

PHPMailer send template in body failed

我需要在我的邮件->正文中发送一个模板,但在退回的邮件中,我没有我的模板,只有“1”。

如果我对我的模板进行回显,我的模板显示良好:

$template = require('../../template/newsletter/template_1.php');
echo $template;

当我尝试像这样在 $mail->body 中调用我的模板时失败了:

$mail->Body = $template;

您需要 return 模板文件中的值,仅要求它是不够的。作为 the docs say:

Successful includes, unless overridden by the included file, return 1.

请特别注意该页面上的示例 5:

return.php
<?php

$var = 'PHP';

return $var;

?>

noreturn.php
<?php

$var = 'PHP';

?>

testreturns.php
<?php

$foo = include 'return.php';

echo $foo; // prints 'PHP'

$bar = include 'noreturn.php';

echo $bar; // prints 1

?>

因此请确保您的模板文件中有一个 return 语句,或者让它输出内容并使用 output buffering 捕获它。或者,使用可为您执行此操作的模板系统,例如 Smarty 或 Twig。