2012-01-13 89 views
-1

我有這個簡單的聯繫形式:奇怪的編碼問題

<form id="emailForm" action="contact.php" method="POST"> 
    <label for="name">Your name</label> 
    <input type="text" id="name" name="name"> 

    <label for="email">Your email</label> 
    <input type="text" id="email" name="email"> 

    <label for="subject">Subject</label> 
    <input type="text" id="subject" name="subject"> 

    <label for="message">Message</label> 
    <textarea id="message" name="message"></textarea> 

    <p class="emailPop" id="emailError"></p> 

    <input id="submit" type="submit" value="Send"> 
</form> 

如果郵件包含像àèìòù Unicode字符,他們會以一種不可思議的方式顯示,當我收到的電子郵件包含我已發送的消息,例如à à à ùòèòòòèà èà à ò

我將表格摘錄到僅包含表單的頁面,並且來自該頁面的消息無需修改就到達了我的電子郵件。經過一番試驗後,我發現問題的原因是標籤<meta charset="utf-8">,這實際上是讓事情發揮作用的標籤。

由於其他頁面使用unicode字符我不能沒有這個標籤,但它會與我的表單的輸出衝突。我該怎麼辦?


這裏是負責發送電子郵件

<?php 
    //require_once 'Mail.php'; 

    function exit_message($error) { 
     echo json_encode(array('status' => 'error', 'message' => $error)); 
     exit(); 
    } 

    $data = $_POST; 

    // Check that all fields are filled in 
    $fields = array('name', 'email', 'subject', 'message'); 

    foreach($fields as $field) { 
     if(empty($data[$field])) 
      exit_message("Please insert your " . $field . '.'); 
    } 

    // Check if email is valid 
    if(!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) 
     exit_message('The email you provided is invalid.'); 

    // Check if message is longer than 9 characters 
    if(strlen($data['message']) <= 9) 
     exit_message('Please write a message at least 9 characters long.'); 

    // Begin composing the message 
    $message = array(
     'recipient' => '[email protected]', 
     'subject' => $data['subject'], 
     'body' => stripslashes($data['message']) . ' - gabrielecirulli.com', 
     'headers' => 'From: "' . $data['name'] . '" <' . $data['email'] . '>' 
    ); 

    // Send 
    if(mail(
     $message['recipient'], 
     utf8_encode($message['subject']), 
     utf8_encode($message['body']), 
     $message['headers'] 
    )) { 
     echo json_encode(array('status' => 'ok')); 
    } else { 
     exit_message('An unidentified error happened while sending your message.'); 
    } 

這裏的PHP腳本的代碼是一個例子:如果我通過我的頁面發送郵件
http://www.gabrielecirulli.com/p/20120113-073417.png

和如果我通過沒有<meta charset="utf-8">的測試頁發送相同的消息:
http://www.gabrielecirulli.com/p/20120113-073503.png

這裏的結果:
http://www.gabrielecirulli.com/p/20120113-073737.png

正如你所看到的頁面沒有meta標籤,其實是給予正確的字符。

此問題出現在Google Chrome和Firefox中。

+0

這是關於您的電子郵件發送機制,而不是您的表單。 – bmargulies 2012-01-13 01:01:44

+0

不,它不是。從沒有meta標籤的頁面發送消息會產生一個理智的電子郵件。當添加meta標籤時,會出現這些符號。 – 2012-01-13 01:05:24

+0

你總是需要指定什麼是編碼。你需要設置元信息告訴瀏覽器哪個編碼解釋文本和在哪個編碼應該發送文本到服務器。對於電子郵件也是如此,您需要設置標題,告訴電子郵件客戶端郵件的編碼方式。所以它*全部是關於您發送電子郵件的方式! – deceze 2012-01-13 01:26:14

回答

2

擺脫utf8_encode爲身體和主題!當你的數據來自瀏覽器時,你的數據已經是UTF-8,你不需要從Latin-1轉換爲UTF-8(這就是utf8_encode所做的)。

您還應該添加適當的標題到指定的編碼的消息:

'headers' => 'From: "' . $data['name'] . '" <' . $data['email'] . ">\r\n" . 
      "MIME-Version: 1.0\r\n" . 
      'Content-type: text/plain; charset=utf-8' 

但也沒有必要stripslashes在身上,除非你有魔行情上,在這種情況下,你應該相當停用Magic Quotes。