2013-12-23 130 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代碼會生成無效的郵件標題。 –