寫 The Paradiso – iCards 程式筆記 Part II

處理 subject, to and cc 上得中文問題

The Paradiso – iCards 裡面有當然有寄信功能. 在該程式我是使用 php mail() function. 如果對中文字沒有額外處理, 將發現寄中文會有問題. 中文字變成亂碼. 我想這應該是程式設計者的困擾吧!? email 內文上面雖然有定義使用 Content-Type: text/html; charset=UTF-8 編碼, 但是只有 email 內文可以正常顯示中文. 而 email 的 subject, to, cc 都是看到亂碼. 仔細看一下正確的編碼應該長得像下列編碼.

1
2
3
4
To: xxxxxx@gmail.com
Subject: =?UTF-8?B?5pu+5bGx5aeG?= has sent you a The Paradiso iCard
From: "=?UTF-8?B?5pu+5bGx5aeG?="
Reply-To: "=?UTF-8?B?5pu+5bGx5aeG?="

那如何產生上列的編碼呢? 可以參考下列兩行程式碼. 就可以解決正常顯示中文

1
2
mb_internal_encoding('utf-8');
$sender_name_encode = mb_encode_mimeheader($sender_name, "UTF-8", "B");

ps. 在台灣的網頁設計師很喜歡使用 Big5 編碼. 我要在此呼籲大家盡量使用 UTF-8 編碼. 唯有這種編碼才是王道. 可以同時顯示中文日文韓文等.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.