字符集 UTF-8 不适用于 <?php 联系表单 [英] Charset UTF-8 not working on <?php contact form

查看:25
本文介绍了字符集 UTF-8 不适用于 <?php 联系表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make utf-8 work when receives the email. When the form is filled up the characters shows as codes if is ç/Ç shows &$ Ex: Avançado shows Avan&$ado

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

I Tried using the " header('Content-Type: text/html;charset=UTF-8'); " but still not working please help me thank you so much...

这是我的代码...

<?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");
    ?>

推荐答案

您不是在那里设置邮件标头,而是设置了 http 标头.此函数 header 正在发送原始 HTTP 标头,它对您发送的电子邮件没有任何作用

You aren't setting the mail header there, you are setting the http header. This function header is sending a raw HTTP header, it isn't doing anything for the email you are sending

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

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

You need to add the header "Content-Type: text/html; charset=UTF-8" (for HTML Email bodies) or "Content-Type: text/plain; charset=UTF-8" (for Plain Text Email bodies) to your mail function. Like this.

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

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

Additionally, for email, each lines should be separated with a CRLF (\r\n) instead of merely using a linefeed (\n). A fully example end result might look more so like this:

<?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);
?>

参考:

这篇关于字符集 UTF-8 不适用于 &lt;?php 联系表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆