[精讚] [會員登入]
2762

[PHP] 處理mail 函式的標題中文字亂碼

PHP的mail函式若標題有中文字得先處理過,否則收信者會看到亂碼

分享此文連結 //n.sfs.tw/10962

分享連結 [PHP] 處理mail 函式的標題中文字亂碼 @新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 00:34:30 最後編修
2017-04-07 11:01:55 By 張○○
 

PHP的mail函式中如果標題(SUBJECT)有中文字,收信者會看到亂碼。

下面是解決標題中文字亂碼的範例:

  $To = "user@example.com";
  $From = "my@example.com";
  $message ="EMAIL 郵件的內容";
  $headers = "MIME-Version: 1.0 \nContent-type: text/html; charset=UTF-8 \n";
  $headers .= "From: {$From}\r\nReply-To: {$From}\r\nX-Mailer: PHP/".phpversion(). "\n";
  $subject = "這是帶有中文字的標題";
  $subject = "=?UTF-8?B?".base64_encode($subject )."?="; 

  $bool = mail($To, $subject, $message, $headers);

其中第7行這裡的 $subject,需要指定編碼和base64 encode處理。


原文 2012-03-24 03:50:01 

END

你可能感興趣的文章

[Smarty5] 如何在樣版中使用php本身的函式 解決 smarty5版後不能直接叫用 php函式的問題

[PHP+ci4] codeignitor4 Cache 及 Session 使用 memcached php 的framework codeignitor4 中的cache和session使用memcached

[PHP>7.3] switch中的 Did you mean to use "continue 2"? 除錯 這近更新 PHP到7.3版以後,出現這樣的錯:targeting switch is equivalent to "break". Did you mean to use "continue 2"?解決方法

[PHP] CodeIgniter 3+pure+smarty安裝及環境設置2/2 PHP framework CodeIgniter 3+ pure CSS +smarty Template Engine的整合#2

[PHP] preg_match 的貪婪和不貪婪比對 在php preg_match中預設是採用貪婪比對,太貪婪反而不符合需要,因此得採用「非貪婪比對」...

[PHP] 移除檔案的UTF8 BOM 移除檔案的UTF8 BOM

隨機好文

[jQuery] select 元件的取值及給值 html中的元件select,在jquery中要如何使用?

SELinux 常用指令和檔案 在Redhat系列中,Centos5以後加入了selinux,他並沒有這麼可怕,不必每次看到Selinux ,就想把他

[AS3] 物件加上超連結的方法 物件加上超連結,可讓使用者點擊的方法

[CKeditor4] 修改調色盤 修改ckeditor預設的調色盤

安裝git centos/freebsd/windows安裝git