2011-11-17 28 views
0

我有一個非常漂亮的HTML模板,現在需要在我的郵件系統中實現。我目前使用這個可以發送電子郵件:將一個HTML電子郵件模板包含到PHP代碼中

$to = $dbuser; 
$subject = "Welcome"; 
$from = "[email protected]"; 
$headers = "From: $from"; 
$server = ""; 

ini_set ("SMTP", $localhost); 

$url=""; 
$msg="$url";  
$body = Example Text! 

mail($to, $subject, $body, $headers); 

如何將包括HTML模板(沿側CSS)直接進入我的PHP電子郵件的形式的$ body變量?

我已經做了相當多的研究,但是我找不到任何實質性的東西。

回答

0

http://php.net/manual/en/function.mail.php - 例如#4

還記得在HTML電子郵件我們強烈建議您使用內聯CSS和老派的HTML格式在可能的情況,以確保與不同的電子郵件客戶端最大的兼容性。也沒有divs - 只是普通的老東西table -s

2

您錯過了電子郵件客戶端將郵件解釋爲HTML所需的標頭。將以下內容添加到您的標題中:

$headers = "From: " . $from . "\r\n"; 
$headers .= "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
0

首先,您需要添加一些標題,以便HTML正確顯示。 從郵件()PHP文檔獲取,你這是怎麼做到這一點:

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

在那之後,我假設$身體是其中文本是應該的,所以它的投入之間的所有HTML的問題引號(用反斜槓轉義HTML中的每個引號),這就是它。我在過去已經使用這樣的

2

的一種方法是,你通常會(使用HTML/PHP等),像這樣創建頁面,然後使用的file_get_contents($網址):

$body = file_get_contents("http://mydomain.com/emailtemplates/template.php?name=John Doe&subject=Hello"); 

因爲你使用的是http://這個php被執行而不是被拉進模板,簡單而有效!

我也建議你使用內聯的CSS,不要害怕使用表!

+0

好主意,但它不工作!當我使用這種方法時,它不會發送!任何想法是爲什麼? – Ben

+0

很難說沒有看到日誌.. 嘗試http://swiftmailer.org/它的一個偉大的電子郵件類,非常強大和易於使用 – homerjam

0

我昨天寫了這個我自己:

  1. 創建HTML並把它放在一個名爲「./email.html」複製和
  2. 文件粘貼下面的代碼的PHP文件在同一dir作爲html文件。如果您在HTML中使用它們
  3. 修改圖像名稱只是這樣做,像這樣:SRC =「CID:world.jpg」

而且完蛋了......我想。 =)

//attachment file paths/names 
$files[0] = './world.jpg'; 
$files[1] = './world2.jpg'; 

$to = ''; 
$bcc = ""; 
$subject = ''; 
        $from = ""; 

$htmlx = ''; 

$handle = @fopen("./email.html", "r"); 
if ($handle) { 
    while (($buffer = fgets($handle, 4096)) !== false) { 
     $htmlx .= $buffer; 
    } 
    if (!feof($handle)) { 
     echo "Error: unexpected fgets() fail\n"; 
    } 
    fclose($handle); 
} 

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x".$semi_rand."x"; 

$headers = "From: $from \n"; 
$headers .= "Reply-To: $from \n"; 
$headers .= 'Bcc: '. $bcc . "\n"; 
$headers .= "MIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . ' boundary="'.$mime_boundary.'"'."\n"; 
$headers .= "X-Author: <Timothy Martens>\n"; 



$message = '--'.$mime_boundary."\n"; 
$message .= 'Content-Type: text/html; charset=UTF-8'."\n"; 
$message .= "Content-Transfer-Encoding: 7bit\n\n\n". $htmlx . "\n\n\n"; 


// preparing attachments 
for($i=0;$i<count($files);$i++){ 
    if(is_file($files[$i])){ 
     $message .= "--".$mime_boundary."\n"; 
     $fp = @fopen($files[$i],"rb"); 
     $data = @fread($fp,filesize($files[$i])); 
     @fclose($fp); 
     $data = chunk_split(base64_encode($data)); 
     $message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" . 
     "Content-Description: ".basename($files[$i])."\n" . 
     "Content-ID: <".basename($files[$i]).">\n". 
     "Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" . 
     "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
    } 
} 
$message .= "--".$mime_boundary."--"; 

if (mail($to, $subject, $message, $headers)) { 
    echo 'Your message has been sent.'."\n"; 
} else { 
    echo 'There was a problem sending the email.'."\n"; 
} 
相關問題