2012-01-13 67 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。