PHP从html消息发送电子邮件

发布于 2024-12-18 02:13:58 字数 823 浏览 0 评论 0原文

我创建了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

成熟的代价 2024-12-25 02:13:58

file_get_contents() 将文件内容读取到字符串中。所以你可以这样做:

$message = file_get_contents('./message_file.html');

请参阅 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:

$message = file_get_contents('./message_file.html');

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.)

知足的幸福 2024-12-25 02:13:58

您应该创建一个 body.php 文件,将消息内容存储在变量中,例如:

$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec dui vitae augue elementum facilisis sit amet in est. Vestibulum lacinia, magna a dictum pretium, mi enim pharetra ligula, at aliquet arcu leo quis libero. Ut ornare dapibus pulvinar. Cras vel nisl nibh. Quisque semper porta augue, in adipiscing elit ultricies eu. Mauris laoreet leo ut justo luctus ut ullamcorper urna feugiat. Vestibulum a massa mauris, sit amet aliquam lorem. Nullam malesuada, neque vel imperdiet aliquet, nisl neque luctus tortor, sed dapibus tellus elit non est. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris vulputate convallis lorem et scelerisque.";

然后 include("body.php"); 位于 mail()调用。

You should create a body.php file with contents of your message stored in a variable, for example:

$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed nec dui vitae augue elementum facilisis sit amet in est. Vestibulum lacinia, magna a dictum pretium, mi enim pharetra ligula, at aliquet arcu leo quis libero. Ut ornare dapibus pulvinar. Cras vel nisl nibh. Quisque semper porta augue, in adipiscing elit ultricies eu. Mauris laoreet leo ut justo luctus ut ullamcorper urna feugiat. Vestibulum a massa mauris, sit amet aliquam lorem. Nullam malesuada, neque vel imperdiet aliquet, nisl neque luctus tortor, sed dapibus tellus elit non est. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Mauris vulputate convallis lorem et scelerisque.";

And then include("body.php"); somewhere above the mail() call.

南七夏 2024-12-25 02:13:58

您可以创建一个文件 body.php 并让它包含如下内容:

$message = 'formatted, escaped and ready message goes here';

并包含该内容。

You can make a file body.php and have it contain something like this:

$message = 'formatted, escaped and ready message goes here';

And include that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文