PHP从html消息发送电子邮件
我创建了一个简单的 php 电子邮件脚本,但问题是我不想在同一个 php 文件中包含大消息文本。我希望脚本从单独的 html 文件中读取消息(与显示的完全相同,带有 html 代码)。我尝试用 include("body.html");
替换该消息,但不幸的是打印了 html 代码而不是发送它。
这是我的脚本的一个小版本
<?php
$to .= '[email protected]';
$subject = 'Message Subject';
$message = 'This is a very important message?';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Person Name <[email protected]>' . "\r\n";
mail($to, $subject, $message, $headers);
?>
I've created a simple php email script but the problem is that I dont want to include the large message text in the same php file. I want the script to read the message (exactly as it appears, with html code) from a separate html file. I've tried to replace the message with include("body.html");
but that unfortunately prints the html code rather than send it.
This is a small version of my script
<?php
$to .= '[email protected]';
$subject = 'Message Subject';
$message = 'This is a very important message?';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Person Name <[email protected]>' . "\r\n";
mail($to, $subject, $message, $headers);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
file_get_contents()
将文件内容读取到字符串中。所以你可以这样做:请参阅 http://php.net/manual/en/function .file-get-contents.php 了解更多信息。
特别是如果文件的内容可能是用户生成的,则这样做的优点是文件的任何部分都不会被解析为 PHP 并在您的服务器上执行。 (当您不是内容的来源时,在向最终用户交付 HTML 时仍然需要担心所有重要的安全问题。)
file_get_contents()
reads the contents of a file into a string. So you could do something like this:See http://php.net/manual/en/function.file-get-contents.php for more information.
Especially if the contents of the file might be user generated, this has the advantage of there being no chance that any part of the file will be parsed as PHP and executed on your server. (There are still all the important security issues to worry about in delivering HTML to end users when you aren't the source of the content.)
您应该创建一个 body.php 文件,将消息内容存储在变量中,例如:
然后
include("body.php");
位于mail()调用。
You should create a body.php file with contents of your message stored in a variable, for example:
And then
include("body.php");
somewhere above themail()
call.您可以创建一个文件
body.php
并让它包含如下内容:并包含该内容。
You can make a file
body.php
and have it contain something like this:And include that.