如何在不执行代码的情况下返回HTML文件的内容


How to return the contents of a HTML file without executing the code?

我有两个文件,一个HTML文件包含电子邮件的布局和基本CSS,另一个PHP文件实际发送电子邮件。我想知道是否有任何方法可以要求或包括HTML文件作为从PHP脚本发送的消息。

例如:

HTML

<h1>A title</h1>

PHP

mail("email", "subject", require('htmlfile'));

我尝试过这样的方法,但它只是将HTML文件的内容放入PHP脚本的流中。有没有什么方法可以让HTML文件只返回文本,我可以将其保存到变量或其他东西中?

提前感谢!

mail("email", "subject", file_get_contents('file_name.html'));

PHP文档状态:Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function.

它给出了一个如何做到这一点的例子,您需要修改代码以满足您的需求。

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

你可以试试这个,它肯定会起作用。

Your PHP file:
"msg.php"
<?php
   $message = "This is a sample message";
?>
Your HTML file with a mail() function:
"mail.php"
<?php
   require ('msg.php');
   mail("sample.yahoo.com", "sample subject", $message);
?> 
<html>
<head>
<body>
<h1>A title</h1>
</body>
</head>
</html>