【问题标题】:Charset UTF-8 not working on <?php contact form字符集 UTF-8 不适用于 <?php 联系表
【发布时间】:2013-09-12 13:21:52
【问题描述】:

我正在尝试在收到电子邮件时使 utf-8 工作。 填写表格后,字符显示为代码 if is ç/Ç 显示 &$ 例如:Avançado 显示 Avan&$ado

我尝试使用“ header('Content-Type: text/html;charset=UTF-8'); ” 但仍然无法正常工作,请帮助我,非常感谢...

这是我的代码...

<?php

    //Get Data do Site
    $name = strip_tags($_POST['name']);
    $email = strip_tags($_POST['email']);
    $service = strip_tags($_POST['service']);
    $phone = strip_tags($_POST['phone']);
    $phoneconfirm = strip_tags($_POST['phoneconfirm']);
    $priority = strip_tags($_POST['priority']);
    $subject = strip_tags($_POST['subject']);
    $message = strip_tags($_POST['message']);

        // Send Message
    header('Content-Type: text/html;charset=UTF-8');
    mail( "THEEMAIL@GOES.HERE", "Via Website",
    "De: $name\n E-Mail: $email\n Serviço: $service\n Telefone/Celular: $phone\n Ligar/Retornar: $phoneconfirm\n Prioridade: $priority\n Assunto: $subject\n Mensagem:\n $message",
    "Para: WebSite");
    ?>

【问题讨论】:

  • 您的问题需要更好的介绍。
  • 向我们展示 HTML 表单,其中名称、电子邮件、服务、电话、电话确认...正在设置。还向我们展示您的 HTML 中的 &lt;meta charset="utf-8"&gt;(它必须存在)

标签: php html utf-8


【解决方案1】:

您没有在此处设置邮件标头,而是在设置 http 标头。这个函数header 正在发送一个原始的 HTTP 标头,它对您发送的电子邮件没有任何作用

   header('Content-Type: text/html;charset=UTF-8');

您需要添加标题 "Content-Type: text/html; charset=UTF-8"(用于 HTML 电子邮件正文)或 "Content-Type: text/plain; charset=UTF-8"(用于纯文本电子邮件正文)到您的 mail 函数。像这样。

$headers = array("Content-Type: text/html; charset=UTF-8");
mail($to, $subject, $message, $headers)

此外,对于电子邮件,每行应使用 CRLF (\r\n) 分隔,而不仅仅是使用换行符 (\n)。一个完整的示例最终结果可能看起来更像这样:

<?php

    $crlf = "\r\n";

    //Get Data
    $name = strip_tags($_POST['name']);
    $email = strip_tags($_POST['email']);
    $service = strip_tags($_POST['service']);
    $phone = strip_tags($_POST['phone']);
    $phoneconfirm = strip_tags($_POST['phoneconfirm']);
    $priority = strip_tags($_POST['priority']);
    $subject = strip_tags($_POST['subject']);
    $message = strip_tags($_POST['message']);

    // Parse/Format/Verify Data
    $to      = "THETOEMAIL@GOES.HERE";
    $from    = 'THEFROMEMAIL@GOES.HERE';
    $subject = "Via Website";
    $message = "De: $name$crlf E-Mail: $email$crlf Serviço: $service$crlf
         Telefone/Celular: $phone$crlf Ligar/Retornar: $phoneconfirm$crlf 
         Prioridade: $priority$crlf Assunto: $subject$crlf Mensagem:$crlf 
         $message";

    // Setup EMAIL headers, particularly to support UTF-8
    // We set the EMAIL headers here, these will be sent out with your message
    // for the receiving email client to use.
    $headers = 'From: ' . $from  . $crlf .
               'Reply-To: ' . $from  . $crlf .
               'Content-Type: text/plain; charset=UTF-8' .  $crlf .
               'Para: WebSite'  .  $crlf .
               'X-Mailer: PHP/' . phpversion();

    // Then we pass the headers into our mail function
    mail($to, $subject, $message, $headers);
?>

参考:

【讨论】:

  • 对不起,我没听懂你说的,你能不能更具体一点,比如解释一下你在我的代码上做了什么
  • 注意:此答案将text/html 指定为标头类型,但实际的邮件正文不是 HTML。您实际上希望 text/plainContent-Type 标头中。但总的来说,我建议避免自己构建标题;它容易出现安全问题,并且难以维护。改用像样的图书馆。
  • Bubba 我现在发送电子邮件时遇到问题,标题都在一行中,我在 $message 中设置的每个函数都需要在另一行中
  • 此代码运行良好,但从 from 获得的所有信息都在一行中,当主题应该从主题表单而不是从表单 $subject = "Via Website";
  • 全部在一行中,因为您告诉电子邮件客户端它是 Content-Type: text/html 而不是 Content-Type: text/plain。您还需要使用 CRLF。你可以随心所欲地创作主题。
【解决方案2】:

以下是我的情况(塞尔维亚语、克罗地亚语、波斯尼亚语……):

在 HTML 页面中: 在你的 email-form.html 的标题中:

<meta http-equiv="Content-Type" content="text/html; charset=windows-UTF-8">

并在“表格”中:

<form name="contactform" method="post" action="http://yourdomain.com/e-mail.php" accept-charset='UTF-8' >    

在 PHP 邮件中:

// create email headers
       $headers = 'From: '.$email_from."\r\n".
       'Content-Type: text/plain; charset=UTF-8' . "\r\n" .
       'Para: WebSite'  .  "\r\n" .
       'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);?>    

【讨论】:

    【解决方案3】:

    我建议使用 Swift Mailer 之类的框架来发送电子邮件,而不是创建自己的电子邮件标头。 这也可能解决字符集问题。

    http://swiftmailer.org/

    【讨论】:

      【解决方案4】:

      header() 函数用于将 HTTP 标头发送到正在加载页面的 Web 浏览器。它对通过mail() 发送的电子邮件内容完全没有任何影响。

      要使用mail() 发送邮件标头,您需要以正确的格式手动构建它们。

      这可能很难正确处理,并且会产生非常混乱的代码,因此我建议您考虑使用像 PHPMailerSwiftmailer 这样的体面的邮件程序类,其中任何一个都允许您指定邮件标题更简单。

      例如,使用 PHPMailer,您的代码可能如下所示:

      $mail = new PHPMailer();
      $mail->From = $from;
      $mail->AddAddress($to);
      $mail->Body = "......";
      $mail->Subject = "....";
      $mail->CharSet="UTF-8";    //see, very easy in phpMailer!
      $mail->Send();
      

      希望对您有所帮助。

      【讨论】:

      • phpmailer 我无法使用它,请您获取我的代码并更改它需要的内容,这样我就可以从这里复制到我的了?我一点也不抱歉
      • @PatrickSterza - 将 PHPMailer 的 URL 添加到答案中。 (github.com/Synchro/PHPMailer)
      【解决方案5】:

      如果您不喜欢使用外部类,可以按照PHP manual page 中的示例进行操作。基本上,您只需将 UTF-8 添加到您的邮件标头(不是浏览器标头)。

      $标头=数组 ( 'MIME 版本:1.0', '内容类型:文本/html; charset="UTF-8";', '内容传输编码:7bit', '日期: ' 。日期('r',$_SERVER['REQUEST_TIME']), '消息ID:', '从: ' 。 $来自, '回复: ' 。 $来自, '返回路径:' 。 $来自, 'X-Mailer: PHP v' 。 php版本(), 'X-起源-IP:'。 $_SERVER['SERVER_ADDR'], ); mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers) );

      【讨论】:

        【解决方案6】:

        我认为这不是电子邮件的问题,是$_POST变量中包含的字符串字符的问题!

        当您将这些字符串从 HTML 表单发送到您的 PHP 时,它们不会使用 UTF8 编码。

        您的 HTML 必须有 &lt;meta charset="utf-8"&gt;,并且当 HTML 从 PHP 呈现时,该 PHP 必须有 header('Content-Type: text/html;charset=UTF-8');

        像这样:

        <?php
         header('Content-Type: text/html;charset=UTF-8');
        ?>
        <html>
        <head>
            ...
            <meta charset="utf8">
            ...
        <body>
        
        <form>
            Name:
            <input type="text" name="name" />
            ...
        
            Service:
            <input type="text" name="service" />
            ...
        
            <submit button>
        </form>
        
        </body>
        </html>
        

        【讨论】:

          【解决方案7】:

          为您的字段尝试此语法。当我尝试发送法语口音时,我遇到了完全相同的问题:

          $variable = utf8_encode(htmlentities($_POST['variable'], ENT_QUOTES, "UTF-8"));
          

          上面采用表单字段并将其放入正确编码为 UTF-8 的变量中。

          对于你的例子,我已经相应地改变了它,所以试试吧:

          <?php
          
          $name           = utf8_encode(htmlentities($_POST['name'], ENT_QUOTES, "UTF-8"));
          $email          = utf8_encode(htmlentities($_POST['email'], ENT_QUOTES, "UTF-8"));
          $service        = utf8_encode(htmlentities($_POST['service'], ENT_QUOTES, "UTF-8"));
          $phone      = utf8_encode(htmlentities($_POST['phone'], ENT_QUOTES, "UTF-8"));
          $phoneconfirm   = utf8_encode(htmlentities($_POST['phoneconfirm'], ENT_QUOTES, "UTF-8"));
          $priority       = utf8_encode(htmlentities($_POST['priority'], ENT_QUOTES, "UTF-8"));
          $subject        = utf8_encode(htmlentities($_POST['subject'], ENT_QUOTES, "UTF-8"));
          $message        = utf8_encode(htmlentities($_POST['message'], ENT_QUOTES, "UTF-8"));
          
          // Send Message
          header('Content-Type: text/html;charset=UTF-8');
          mail( "THEEMAIL@GOES.HERE", "Via Website",
          "De: $name\n E-Mail: $email\n Serviço: $service\n Telefone/Celular: $phone\n Ligar/Retornar: $phoneconfirm\n Prioridade: $priority\n Assunto: $subject\n Mensagem:\n $message",
          "Para: WebSite");
          
          ?>
          

          希望对你有所帮助!

          祝你好运, 米奇。

          【讨论】:

          • 对不起哥们,当我点击按钮发送时它不能正常发送,甚至无法发送 =(
          • @PatrickSterza 那是因为出现了错误。现在就试试。我已经更新了代码!
          • 当我点击发送时仍然不发送不会加载这个代码的contact.php页面。地雷工作但它没有显示像çÇáàé等字符......
          • @PatrickSterza 会发生什么?显示任何错误?我需要的不止这些。我拿了你的确切代码,只是用这些新转义的变量替换了变量。你有 Firefox 的 Firebug 吗?使用 NET 选项卡查看传递了哪些变量?
          • 天啊,我的发送按钮它没有发送,我点击发送消息,它什么也没做我做了什么?
          猜你喜欢
          • 2014-05-20
          • 2013-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多