Demo

討論

保持飢餓,保持愚笨 (Stay hungry, stay foolish)。 史蒂夫·賈伯斯

公告

本網站已取消討論區的討論方式,如果您有問題請至LINE群行動條碼圖片網址,或Facebook社團

有關UTF-8發送電郵到不同webmail的對應

  • mickey9801
  • mickey9801 的個人頭像 Topic Author
  • 訪客
  • 訪客
2005-03-13 15:52 - 2005-03-13 15:54 #114 來自 mickey9801
mickey9801 created the topic: 有關UTF-8發送電郵到不同webmail的對應
Hi, 初到貴境,先來報到。請多指教。

最近一直研究在UTF-8中發送中文電郵到yahoo之類的webmail時會出現問題,做了一個網頁來測試對應不同webmail和一般使用的outlook express的特性。我想可以作為正在致力進行UTF-8中文化的同志提供一點點參考,所以先拿出來分享一下。由於小弟所工作的是個跟電玩有關的香港網站,常會出現繁體中文、日文和香港增補字集混用的情況,單用iconv很多時候都會出問題,所以這個網頁是以無論有甚麼國家字元放進去都行得通的做法去做。

老實說,幾乎每一個webmail都有不同的脾性,小弟實在不知道有沒有一個統一的方式可以對應到所有電郵中,所以只有用最蠢的 if 方式來決定對應方式,現在只對應hotmail,yahoo和gmail,新浪也只是測試性質(因為那個電郵系統實在太爛了...)。其中最笨的,要算是hotmail,竟然要將信件內容轉回big5去...另外,yahoo的測試中發現如果將電郵forward到一般電郵程式上閱讀,會變成一堆&#xxxxx;的unicode碼,需要收信人自己處理(當然,干脆使用html電郵就可以解決問題,但有部份電郵程式,尤其是大學的老古董都無法看,所以我還是堅持以text/plain格式寄信)理所當然地,一些小弟不知道的webmail就可能會出現不對應的情況了。 如果各位大大有甚麼心得的話,請分享一下。

另外,這個網頁還有一點地方使用了mbstring extension,不過知道很多網頁寄存空間都沒有開啟這extension的功能,所以可能的話,也想自己編寫個不用mbstring也能正確計算utf-8字串字數和選取utf-8字串部份的函式。這點由於小弟在忙著別的事情,留待容後再處理了。

sendmail_utf8.php
<?php /* Apache 設定 AddDefaultCharset UTF-8 PHP 設定 default_charset = "UTF-8" 請確認/etc/php.ini中mbstring.func_overload = 0,否則會出問題 */ // Work around for "current" Apache 2 + PHP module which seems to not // cope with private cache control setting if (!empty($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache/2')) { header ('Cache-Control: no-cache, pre-check=0, post-check=0'); } else { header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0'); } header ('Expires: 0'); header ('Pragma: no-cache'); // Set page encode header ('Content-Type: text/html; charset=utf-8'); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>電郵傳送測試</title> </head> <body> <h3>電郵傳送測試</h3><h4> UTF-8 + ASCII encode + Webmail Filtering Ver.</h4> <font color=red> <?php // check if submit from form if (isset($_POST["Send"])) { //setup smtp server $mailserver = $_POST["SMTP"]; if ($mailserver != "") ini_set("SMTP", $mailserver); //get data from form $to = $_POST["To"]; $sender = $_POST["sender"]; $reply_to = $_POST["reply_to"]; $subject = $_POST["Subject"]; $body = $_POST["TextBody"]; // sprin receivers name into array $receiver_array = explode(";", $to); foreach($receiver_array as $receiver) { $receiver = trim($receiver); if (strpos($receiver, "hotmail.com")) { // Hotmail $mail_sender = base64_encode(ascii_encode($sender)); $mail_subject = base64_encode(ascii_encode($subject)); $mail_body = get_hotmail_body($body); $content_type = "text/plain"; $charset = "big5"; } else if (strpos($receiver, "yahoo.com")) { // Yahoo Mail $mail_sender = base64_encode(ascii_encode($sender)); $mail_subject = base64_encode(ascii_encode($subject)); $mail_body = ascii_encode($body); $content_type = "text/plain"; $charset = "utf-8"; } else if (strpos($receiver, "sina")) { // Sina Mail (not tested) $mail_sender = base64_encode(ascii_encode($sender)); $mail_subject = base64_encode(ascii_encode($subject)); $mail_body = ascii_encode($body); $content_type = "text/plain"; $charset = "utf-8"; } else if (strpos($receiver, "gmail.com")) { // GMail $mail_sender = base64_encode($sender); $mail_subject = base64_encode($subject); $mail_body = $body; $content_type = "text/plain"; $charset = "utf-8"; } else { // Others, $mail_sender = base64_encode($sender); $mail_subject = base64_encode($subject); $mail_body = $body; $content_type = "text/plain"; $charset = "utf-8"; } //encode from[code]
<?php
/*
Apache 設定 AddDefaultCharset UTF-8
PHP 設定 default_charset = "UTF-8"
請確認/etc/php.ini中mbstring.func_overload = 0,否則會出問題
*/



// Work around for "current" Apache 2 + PHP module which seems to not
// cope with private cache control setting
if (!empty($_SERVER) && strstr($_SERVER, 'Apache/2'))
{
header ('Cache-Control: no-cache, pre-check=0, post-check=0');
}
else
{
header ('Cache-Control: private, pre-check=0, post-check=0, max-age=0');
}
header ('Expires: 0');
header ('Pragma: no-cache');
// Set page encode
header ('Content-Type: text/html; charset=utf-8');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>電郵傳送測試</title>
</head>

<body>
<h3>電郵傳送測試</h3><h4> UTF-8 + ASCII encode + Webmail Filtering Ver.</h4>
<font color=red>
<?php
// check if submit from form
if (isset($_POST["Send"])) {
//setup smtp server
$mailserver = $_POST["SMTP"];
if ($mailserver != "")
ini_set("SMTP", $mailserver);
//get data from form
$to = $_POST["To"];
$sender = $_POST["sender"];
$reply_to = $_POST["reply_to"];
$subject = $_POST["Subject"];
$body = $_POST["TextBody"];

// sprin receivers name into array
$receiver_array = explode(";", $to);
foreach($receiver_array as $receiver)
{
$receiver = trim($receiver);
if (strpos($receiver, "hotmail.com")) { // Hotmail
$mail_sender = base64_encode(ascii_encode($sender));
$mail_subject = base64_encode(ascii_encode($subject));
$mail_body = get_hotmail_body($body);
$content_type = "text/plain";
$charset = "big5";
}
else if (strpos($receiver, "yahoo.com")) { // Yahoo Mail
$mail_sender = base64_encode(ascii_encode($sender));
$mail_subject = base64_encode(ascii_encode($subject));
$mail_body = ascii_encode($body);
$content_type = "text/plain";
$charset = "utf-8";
}
else if (strpos($receiver, "sina")) { // Sina Mail (not tested)
$mail_sender = base64_encode(ascii_encode($sender));
$mail_subject = base64_encode(ascii_encode($subject));
$mail_body = ascii_encode($body);
$content_type = "text/plain";
$charset = "utf-8";
}
else if (strpos($receiver, "gmail.com")) { // GMail
$mail_sender = base64_encode($sender);
$mail_subject = base64_encode($subject);
$mail_body = $body;
$content_type = "text/plain";
$charset = "utf-8";
} else { // Others,
$mail_sender = base64_encode($sender);
$mail_subject = base64_encode($subject);
$mail_body = $body;
$content_type = "text/plain";
$charset = "utf-8";
}

//encode from
最後修改原因: 2005-03-13 15:54 來自 mickey9801.

Please 登入 to join the conversation.

More
2005-03-18 14:04 #152 來自 seethrou
seethrou replied the topic: 有關UTF-8發送電郵到不同webmail的對應
先收下! :)

有時間再研究,
希望大大有更好消息, ;)

bye!
HKer

思富交流 - 内容管理, 電子商務, 購物車, 商城, 系統管理, 網路應用
Joomla! CMS, Zen Cart Shopping Cart, eCommerce, Web,...

Please 登入 to join the conversation.