2011-12-19 90 views
2

我創建了一個html電子郵件welcome.tpl我必須使用什麼PHP郵件方法才能將該文件作爲郵件正文發送出去?在此之前,我一直在使用,幷包括變量處理HTML電子郵件模板文件和php

$text_content.= "\r\n"; 
$text_content.= "--------------------------------\r\n"; 
$html_content.= "</body></html>"; 

$mime_boundary = 'Multipart_Boundary_x'.md5(time()).'x'; 

$headers = "MIME-Version: 1.0\r\n"; 
$headers.= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\r\n"; 
$headers.= "Content-Transfer-Encoding: 7bit\r\n"; 

$body = "--$mime_boundary\n"; 
$body.= "Content-Type: text/plain; charset=\"charset=us-ascii\"\n"; 
$body.= "Content-Transfer-Encoding: 7bit\n\n"; 
$body.= $text_content; 
$body.= "\n\n"; 

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

$headers.= 'From: So <[email protected]>' . "\r\n"; 
$headers.= "X-Sender-IP: $_SERVER[SERVER_ADDR]\r\n"; 
$headers.= 'Date: '.date('n/d/Y g:i A')."\r\n"; 
$headers.= 'Reply-To: So <[email protected]>' . "\r\n"; 

mail($en['email'], $subject, $body, $headers); 

HTML和文字,我應該使用類似$body = file_get_contents();,是mail();最好的方法是什麼?

+0

的看看?你可以顯示welcome.tpl和郵件功能的重要部分嗎? – Jomoos 2011-12-19 11:21:07

+0

@Jomoos我已經添加了我目前的郵件()函數的片段...至於welcome.tpl它只是基本的HTML使用所需的電子郵件只能接受的HTML/CSS – acctman 2011-12-19 16:26:46

回答

2

使用下面的代碼:

//發送HTML郵件

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

時,設置的內容類型,你還可以包括 //更多標題

$headers .= 'From: <[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 

和使用

$body = file_get_contents(); 

,並通過郵件功能發送郵件:

mail($en['email'], $subject, $body, $headers); 
2

我建議如下:

你正在使用你的模板.tpl擴展,所以我假設你正在運行的Smarty作爲模板發動機?

如果沒有,那麼你可以簡單地使用file_get_contents();

$template = file_get_contents('template.tpl'); 
$template = str_replace('{name}', 'Sean Nieuwoudt', $template); 
$template = str_replace('{email}', '[email protected]', $template); 
... 
etc 

的簡單地使用mail()函數發送過電子郵件。

另一種更可靠的方法是使用類似Postmarkapp的東西來發送電子郵件。它可以確保郵件的發送位置在郵件收件人垃圾郵件文件夾中(特別是在共享主機環境中運行時)。

郵戳,你可以做這樣的事情:

Mail_Postmark::compose() 
    ->addTo('[email protected]', 'Jane Smith') 
    ->subject('Subject') 
    ->messagePlain($template) 
    ->send(); 

一些免費提供的PHP-郵戳類http://developer.postmarkapp.com/developer-libs.html#php-5您正在使用哪個模板庫