PHP mail()寄出HTML信件問題研究

使用php的mail()寄出信件時,遇到以下兩種狀況:

一、沒有使用附件,但是附件固定會夾帶一個名為ATT00001.txt的檔案

解決方法:
在$mail_content 裡面把第二行的$content .= "--$mime_boundary\n";移除即可

$message = $mail_content;
    $mime_boundary = md5(uniqid(mt_rand(), TRUE));

    //郵件標頭
    $header = "From: $from_name<$from_email>\n";
    //$header .= "To: $to_name<$to_email>\n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "Content-Type: multipart/mixed; boundary=". $mime_boundary . "\n";
    if (count($mail_cc_array) > 0)
    {
        $header .= 'Cc:'.implode(',',$mail_cc_array)."\n";
    }
    if (count($mail_bcc_array) > 0)
    {
        $header .= 'Bcc:'.implode(',',$mail_bcc_array);
    }

    //建立郵件內容的前端
    $content = "This is a multi-part message in MIME format.\n";
    $content .= "--$mime_boundary\n";
    $content .= "Content-Type: text/$format; charset=utf-8\n";
    $content .= "Content-Transfer-Encoding: 8bit\n\n";
    $content .= "$message\n";
    $content .= "--$mime_boundary\n";

    $Order_Ym = date('Ym',strtotime($key_end_date));
    $limit_Ym = date('Ym',strtotime($key_start_date));
  //信件標題
    $subject = "標題";
     $subject = "=?utf-8?B?" . base64_encode($subject) . "?=";

     //一切無誤時傳送郵件
     $mail_status = mail($to_email, $subject, $mail_content, $header);


二、傳入資料變數並寄出後,發現某些欄位出現亂碼,如圖:


現況分析:

Q2部分,只要:後面是空白沒有值的話,看起來是跟HTML編碼組合起來,變成一個奇怪的字串,造成顯示異常

目前處理:
trim()
strip_tags() //過濾所有html標籤
addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。

留言

熱門文章