1

Hello The Genious peoples.... 我正在尝试使用 php 发送 html 邮件,我的 php 脚本中的所有内容看起来都是正确的,但是当我发送邮件时,我只收到纯文本。这是我用来测试脚本的链接 url 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 天开始就在尝试..

4

1 回答 1

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 />";
?> 
于 2012-09-06T16:32:45.403 回答