用PHP将网页通过电子邮件发送

4

与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获取数据并将其发送为电子邮件正文。

我想知道是否在PHP中有类似(CreateMHTMLBody)的函数?

如果没有,我们能否创建任何函数?如果可以,请给我一些提示。

谢谢

4个回答

9
下面是一个例子:

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

3
可以用这个方法,但并不完美。有没有一种方法可以生成页面的某种“照片”,提取其中的图片和CSS布局?我搜索了一个PHP库,但没有找到任何内容。 - kevin

4

补充Erik的回答,如果你想导入本地(或远程!)文件而不是在代码中指定HTML,可以这样做:

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

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

3
使用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();

1

操作步骤如下:

$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);

您刚刚发送了HTML电子邮件。 若要加载外部HTML文件,请将 $message ='' 替换为:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接