PHP邮件功能不发送HTML邮件

时间:2012-09-06 16:26:40

标签: php email

你好,各族人民...... 我试图使用PHP发送一个HTML邮件我的PHP脚本中的一切看起来是正确的但是当我发送邮件时我只收到纯文本。这是我用来测试脚本http://www.mailme.netne.net的链接网址 你可以在这里查看..

这些是脚本中的代码:

<?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天..

1 个答案:

答案 0 :(得分: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 />";
?>