2013-12-23 45 views
-1

我的项目中有以下代码。问题在于邮件(邮件正文)显示不正确。Imap邮件无法正常工作

--37cd8764df1f86bc509d0994d83f1d26 Content-type: text/plain;charset="iso-8859-1" Content-Transfer-Encoding: 7bit hello --37cd8764df1f86bc509d0994d83f1d26 Content-type: text/html;charset="iso-8859-1" Content-Transfer-Encoding: 7bit hello --37cd8764df1f86bc509d0994d83f1d26-- 

它在电子邮件中显示上述结果。其中“你好”是正文。

另一个问题是,如果我附加文件的身体不显示,并正确发送附件。我有以下代码。

<?php 
session_start(); 
ob_start(); 
?> 
<?php 
include("validation/lg_check.php"); 
?> 
<?php 
$to1=trim($_POST['txt_to']); 
$sub=trim($_POST['txt_sub']); 
$msg=$_POST['txt_body']; 
$_SESSION['err_mail']=array(); 

if(empty($to1)) 
{ 
$_SESSION['err_mail']['to']="Please Enter The Recepient"; 
exit; 
} 
$imap = imap_open("{myserver}", $_SESSION['user']); 
$return_path = $_SESSION['user']; 
$to = $to1; 
$subject = $sub; 

# Get a random 32 bit number using time() as seed. 

$boundary=md5(date('r',time())); 
$message=' --'.$boundary."\n"; 
$message .='Content-type: text/plain;charset="iso-8859-1"'."\n"; 
$message .='Content-Transfer-Encoding: 7bit'."\n\n"; 
$message .=$msg."\n"; 
$message .='--'.$boundary."\n"; 
$message .='Content-type: text/html;charset="iso-8859-1"'."\n"; 
$message .='Content-Transfer-Encoding: 7bit'."\n\n"; 
$message .=$msg."\n"; 
$message .='--'.$boundary.'--'; 

if(isset($_FILES['file_upload']) && !empty($_FILES['file_upload'])) 
{ 
$fnm=time()."_".$_FILES['file_upload']['name']; 
$tmp_file=$_FILES['file_upload']['tmp_name']; 
$file_upload=move_uploaded_file($tmp_file,"upload/".$fnm); 

} 

if($file_upload) 
{ 
# Define the attachment section 
# Open a file 
$file_name="upload/".$fnm; 
$file = fopen($file_name, "r"); 
if($file == false) 
{ 
echo "Error in opening file"; 
exit(); 
} 
# Read the file into a variable 
$size = filesize("upload/".$fnm); 
$content = fread($file, $size); 
# encode the data for safe transit 
# and insert \r\n after every 76 chars. 
$encoded_content = chunk_split(base64_encode($content)); 
$headers .= "Content-Type: application/octet-stream; name=".$fnm." "; 
$headers .= "name=".$fnm."\r\n"; 
$headers .= "Content-Transfer-Encoding:base64\r\n"; 
$headers .= "Content-Disposition: attachment;"; 
$headers .= "filename=".$fnm."\r\n\n"; 
$headers .= "$encoded_content\r\n"; 
$headers .= "--$num--"; 
}  

# Send email now 
$retval = imap_mail($to,$subject,$message,$headers, $return_path); 
if($retval == true) 
{ 
echo "Message sent successfully..."; 
/* print $header; 
print $message; 
echo '<br>'; 
print $to; 
echo '<br>'; 
print $subject; 
echo '<br>'; 
print $message; 
echo '<br>'; 
print $fnm; */ 
} 
else 
{ 
echo "Message could not be sent..."; 
} 
?> 

回答

0

如果您试图在一个邮件正文内发送多个编码,您首先必须告诉客户端您已这么做,以及您的界限实际上是什么。

因此,在开始使用多个消息体之前,必须将其包含到具有参数:“boundary =”---您的边界---“的Content-type:multipart/alternative”中:

$message='Content-Type: multipart/alternative; 
    boundary="' . $boundary . '"' . "\r\n" . "\r\n"; 

而你需要通过CR和LF终止每行! 和标题需要的是两次分裂脱离主体。

你真的应该阅读邮件头的RFC,这很烦人,但我知道,但发送格式不正确的电子邮件也是如此。还有更多的邮件客户端比MS Outlook!

+0

谢谢你的回复。但是我对PHP很陌生,所以请你更新导致错误的地区。 –

+0

这不是php的问题,邮件头设置错了。您正确的php代码会生成无效的邮件标题。 –