0

嘿,我这里有这段代码,我想做的是将这个 html 文件中的内容包含到电子邮件中,但它似乎不起作用:( mime 类型好吗?是 php 不包括什么我在寻找?

        $to = "email@domainname.com";
        $subject = "Late Notice";
        $message .= include("latenotice.html");
        $from = "myfriend@hisdomainname.com";
        $headers = "From:" . $from;
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        mail($to,$subject,$message,$headers);
4

4 回答 4

3

include doesn't return a string. It executes the file, which means the HTML will be echoed to the screen.

Try file_get_contents instead.

$message .= file_get_contents("latenotice.html");

Note: This will not execute any PHP code in the file. If you want that, you can use output buffering.

ob_start();
include("latenotice.html");
$message .= ob_get_clean();
于 2012-04-20T18:34:02.470 回答
1

include only evaluates to a value when the included file returns something - latenotice.html probably just contains the content. I think you want to read the file:

$message .= file_get_contents("latenotice.html");
于 2012-04-20T18:32:57.417 回答
1

Include doesn't return a string, it includes executable code in the script.

You are probably looking for file_get_contents:

$message .= file_get_contents('latenotice.html');

Also, you should look at a PEAR lib like Mail_Mime; attachments and mime types are not trivial to get right.

于 2012-04-20T18:33:40.227 回答
1

如果您希望在包含后“返回”某些内容,则需要在包含的文件末尾添加一个返回。

像file.php

 return "My String";

电子邮件.php

$var = include('file.php'); // "My String"

在您的情况下使用file_get_contents

于 2012-04-20T18:31:56.430 回答