Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中通过电子邮件发送网页_Php_Email - Fatal编程技术网

在php中通过电子邮件发送网页

在php中通过电子邮件发送网页,php,email,Php,Email,在asp中,我们可以通过电子邮件发送完整的网页,这基本上为开发人员节省了大量创建和发送电子邮件的时间 请参阅下面的代码 <% Set myMail=CreateObject("CDO.Message") myMail.Subject="Sending email with CDO" myMail.From="xxx@example.com" myMail.To="xxx@example.com" myMail.CreateMHTMLBody

在asp中,我们可以通过电子邮件发送完整的网页,这基本上为开发人员节省了大量创建和发送电子邮件的时间

请参阅下面的代码

     <%
    Set myMail=CreateObject("CDO.Message")
    myMail.Subject="Sending email with CDO"
    myMail.From="xxx@example.com"
    myMail.To="xxx@example.com"
    myMail.CreateMHTMLBody "mywebpage.html",cdoSuppressNone
    myMail.Send
    set myMail=nothing
    %>

正如我们所知,CreateMHTMLBody将从mywebpage.html获取数据,并将其作为电子邮件正文发送

我想知道像(CreateMHTMLBody)这样的函数在php中是否可用

如果没有,我们可以包装任何功能如果是的,请给我一些提示

谢谢

以下是方法:

$to  = 'joe@example.com';
$subject = 'A test email!';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Put your HTML here
$message = '<html><body>hello world</body></html>';

// Mail it
mail($to, $subject, $message, $headers);
示例如下:

<?
    if(($Content = file_get_contents("somefile.html")) === false) {
        $Content = "";
    }

    $Headers  = "MIME-Version: 1.0\n";
    $Headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $Headers .= "From: ".$FromName." <".$FromEmail.">\n";
    $Headers .= "Reply-To: ".$ReplyTo."\n";
    $Headers .= "X-Sender: <".$FromEmail.">\n";
    $Headers .= "X-Mailer: PHP\n"; 
    $Headers .= "X-Priority: 1\n"; 
    $Headers .= "Return-Path: <".$FromEmail.">\n";  

    if(mail($ToEmail, $Subject, $Content, $Headers) == false) {
        //Error
    }
?>

要添加到Erik的答案中,如果要导入本地(或远程!)文件而不是在代码中指定HTML,可以执行以下操作:

// fetch locally
$message = file_get_contents('filename.html');

// fetch remotely
$message = file_get_contents('http://example.com/filename.html');

使用PHP的输出缓冲区函数并包含所需的网页。例如:

// Start output buffering
ob_start();

// Get desired webpage
include "webpage.php";

// Store output data in variable for later use
$data = ob_get_contents();

// Clean buffer if you want to continue to output some more code
// in which case it would make sense to use this functionality in the very beginning
// of your page when no other code has been processed yet.
ob_end_clean();

这是可行的,但并不完美。有并没有一种方法可以提取带有图片和css布局的页面的某种“照片”?我在谷歌上搜索了一个php库,但什么也找不到。
// Start output buffering
ob_start();

// Get desired webpage
include "webpage.php";

// Store output data in variable for later use
$data = ob_get_contents();

// Clean buffer if you want to continue to output some more code
// in which case it would make sense to use this functionality in the very beginning
// of your page when no other code has been processed yet.
ob_end_clean();