我正试图用另一个PHP文件创建的HTML生成一封电子邮件。
电子邮件生成文件由每小时运行一次的cron运行。存在另一个生成电子邮件所需HTML的文件。
HTML生成文件没有我可以调用的函数,例如:
$emailBody = generateHTML($id);
HTML生成文件被设计为包含在您希望显示HTML的页面上,例如:
include "htmlGenerator.php";
我的问题是:我如何才能将htmlgenerator.php文件返回到变量中,并准备推送到电子邮件中。
抱歉,如果这不是很清楚,我很乐意回答任何问题。
提前感谢!
如果我理解你所说的,你可以使用输出缓冲,以便您可以获得htmlGenerator.php
的输出
例如:
ob_start();
// as an example
echo "Hello World";
// or in our case
include "htmlGenerator.php";
$result = ob_get_contents();
ob_end_clean();
您还可以创建一个简单的函数,如下所示:
function include_output($filename)
{
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
$mycontent = include_output("htmlGenerator.php");
有关更多详细信息,请阅读php文档。
查看内置函数ob_start和ob_get_clean。
然后你可以做一些类似的事情:
ob_start();
include( "file.php");
$var = ob_get_clean();