PHP發email信件 mail 函數標題編碼問題

用PHP mail函數 發信使用unicode,在某些收信軟體會出現標題問號?的問題,可用base64_encode函數來解決:
如果
$to = 'goal@test.com';
$subject = 'test subject encode';
$mailmsg = 'test centent encode';
$header = 'Content-type: text/html; charset=UTF-8; Content-Transfer-Encoding';
$para = '-f'.'test@test.com';
mail($to,subject,$mailmsg,$header,$para);

寄出後,若於MS Outlook等收信軟體或App遇到標題 ????? 等字串,則可利用以下方式修正:

function StringEncode($code,$string)
{
  $encodestring = "=?$code?B?".base64_encode($string)."?="
/* 透過base64來預編UTF-8 碼 ,除標題亦可用在寄件者與收件者名稱,如:收件者<goal@test.com> */
  return $encodestring; 
 }

$to = 'goal@test.com'; 
$subject = StringEncode('UTF-8','test subject encode');
$mailmsg = 'test centent encode';
$header = 'Content-type: text/html; charset=UTF-8';
$para = '-f'.'test@test.com';
mail($to,subject,$mailmsg,$header,$para);


參考資料
https://stackoverflow.com/questions/454833/system-net-mail-and-utf-8bxxxxx-headers

https://ncona.com/2011/06/using-utf-8-characters-on-an-e-mail-subject/

這個網誌中的熱門文章

Upgrade php and httpd(apache) on CentOS 7

Installing VMware workstation pro on Ubuntu 22.X (Jammy Jellyfish)