-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...";
}
?>
謝謝你的回覆。但是我對PHP很陌生,所以請你更新導致錯誤的地區。 –
這不是php的問題,郵件頭設置錯了。您正確的php代碼會生成無效的郵件標題。 –