2009-12-31 81 views
3

發送網頁,如ASP,我們有功能,在郵件發送完整的網頁,基本上節省大量的時間用於開發人員創建&發送電子郵件通過電子郵件在PHP

看到下面的代碼

 <% 
    Set myMail=CreateObject("CDO.Message") 
    myMail.Subject="Sending email with CDO" 
    myMail.From="[email protected]" 
    myMail.To="[email protected]" 
    myMail.CreateMHTMLBody "mywebpage.html",cdoSuppressNone 
    myMail.Send 
    set myMail=nothing 
    %> 

因爲我們知道CreateMHTMLBody將從mywebpage.html獲取數據並將其作爲電子郵件的主體發送。

我想知道是否有任何功能像(CreateMHTMLBody)這是在PHP中可用?

如果是,我們是否可以打包任何功能,請給我一些提示。

感謝

回答

6

例子:(!或遠程)

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

雖然有效,但並不完美。有沒有辦法讓圖片和CSS佈局被提取的頁面的某種「照片」? 我搜索了一個php庫,但找不到任何東西。 – kevin 2010-04-26 15:16:52

1

方法如下:

$to = '[email protected]'; 
$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 = file_get_contents('the_file.html'); 
3

爲了增加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();