2012-09-06 90 views
1

你好Genious人.... 我想發送一個HTML郵件使用PHP一切在我的PHP腳本看起來是正確的,但是當我發送郵件時,我只收到純文本。這是鏈接的URL我使用測試腳本http://www.mailme.netne.net 您可以點擊此處查看..PHP郵件功能不發送HTML郵件

這些都是腳本代碼:

<?php 
require 'ErrorHandler.inc.php'; 
//.....set up a boundary to seperate the message.......... 
$boundary = '======'.md5(mt_rand(4,time())).'======'; 
$headers=array(); 
$headers[]='MIME-Version:1.0'; 
$headers[]='Content-type:multipart/alternative;boundary="'.$boundary.'"'; 
$headers[]='From: '.$_POST['from']; 
$msg_body = 'This a is Multipart Message in MIME Format'."\n"; 
$msg_body .= '--'.$boundary."\n"; 
$msg_body .= 'Content-Type:text/html; charset="iso-8859-1"'."\n"; 
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n"; 
$msg_body .=$_POST['message'] ."\n"; 
$msg_body .= '--'.$boundary."\n"; 
$msg_body .= 'Content-Type:text/plain; charset="iso-8859-1"'."\n"; 
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n"; 
$msg_body .=strip_tags($_POST['message']) . "\n"; 
$msg_body .= '--'.$boundary.'--'."\n"; 
//======================send n test return value======================= 
$success = mail($_POST['to'],$_POST['sub'],$msg_body,implode("\r\n",$headers)) ; 
if($success) 
{ echo "<strong>Your mail sent </strong>";}  
else  
{echo "<strong>Error Sending your mail..please try again later</strong>";} 
echo " with following details:<br /><br />"; 
echo "<strong>From : </strong><em>" .$_POST['from']."<br />"; 
echo "<strong>To&nbsp;&nbsp;: </strong><em>".$_POST['to']. "<br/>"; 
echo "<strong>Subject : </strong><em>".$_POST['sub']."<br />"; 
echo "<strong>Message : </strong><em>".$msg_body."<br />"; 
?>  

請幫我從去年6 ..我正在嘗試天..

+0

您可能需要使用http://swiftmailer.org/,而不是寫你自己的。 –

+1

這不是您的問題的答案,但您應該再次考慮將用戶輸入的文本發送到用戶輸入的電子郵件地址的腳本。事實上,垃圾郵件發送者可能會濫用它。 – andrewsi

+0

有很多圖書館可以爲你構建電子郵件,比如PHPMailer。您最好使用其中的一種,而不是試圖將電子郵件連接在一起。 –

回答

1

您要添加標題數據添加到您的郵件正文:

$msg_body .= 'Content-Type:text/html; charset="iso-8859-1"'."\n"; 
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n"; 

但你沒有真正將它添加到你的頭,像這樣:

$headers[] = 'Content-Type:text/html; charset="iso-8859-1"'; 
$headers[] = 'Content-Transfer-Encoding:7bit'; 

試試這個:

<?php 
require 'ErrorHandler.inc.php'; 

//.....set up a boundary to seperate the message.......... 
$boundary = '======'.md5(mt_rand(4,time())).'======'; 

$headers = array(); 
$headers[] = 'MIME-Version:1.0'; 
$headers[] = 'Content-type:multipart/alternative;boundary="'.$boundary.'"'; 
$headers[] = 'From: '.$_POST['from']; 
$headers[] = 'Content-Type:text/html; charset="iso-8859-1"'; 
$headers[] = 'Content-Transfer-Encoding:7bit'; 

$msg_body = 'This a is Multipart Message in MIME Format'."\n"; 
$msg_body .= '--'.$boundary."\n"; 
$msg_body .= 'Content-Type:text/html; charset="iso-8859-1"'."\n"; 
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n"; 
$msg_body .=$_POST['message'] ."\n"; 
$msg_body .= '--'.$boundary."\n"; 
$msg_body .= 'Content-Type:text/plain; charset="iso-8859-1"'."\n"; 
$msg_body .= 'Content-Transfer-Encoding:7bit'."\n\n"; 
$msg_body .=strip_tags($_POST['message']) . "\n"; 
$msg_body .= '--'.$boundary.'--'."\n"; 


//======================send n test return value======================= 
$success = mail($_POST['to'],$_POST['sub'],$msg_body,implode("\r\n",$headers)) ; 
if($success){ 
    echo "<strong>Your mail sent </strong>"; 
}  
else  
{ 
    echo "<strong>Error Sending your mail..please try again later</strong>";  
} 

echo " with following details:<br /><br />"; 
echo "<strong>From : </strong><em>" .$_POST['from']."<br />"; 
echo "<strong>To&nbsp;&nbsp;: </strong><em>".$_POST['to']. "<br/>"; 
echo "<strong>Subject : </strong><em>".$_POST['sub']."<br />"; 
echo "<strong>Message : </strong><em>".$msg_body."<br />"; 
?> 
+0

感謝您使用INterst,但是這樣做會如何。這樣,我們只需在頭文件中添加內容類型聲明兩次,然後再在msg_body中添加內容類型聲明。 –