字符集 UTF-8 不适用于<?php 联系表单


Charset UTF-8 not working on <?php contact form

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

我尝试使用" header('内容类型: 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");
    ?>

你不是在那里设置邮件头,而是在设置 http 头。此函数header发送原始HTTP标头,它不会对您发送的电子邮件执行任何操作

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

您需要添加标题"内容类型:文本/html;charset=UTF-8"(用于 HTML 电子邮件正文)或"内容类型:文本/纯文本;charset=UTF-8"(用于纯文本电子邮件正文)到您的mail函数。喜欢这个。

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

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

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

参考:

  • 标头函数
  • 邮件功能

以下是它在我的情况下的工作方式(塞尔维亚语、克罗地亚语、波斯尼亚语等):

在 HTML 页面中:在您的电子邮件表单的标题中.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);?>    

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

http://swiftmailer.org/

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

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

这可能很难正确,并且可能会使代码变得非常混乱,所以我建议你考虑使用一个体面的邮件类,如PHPMailer或Swiftmailer,其中任何一个都允许你以更简单的方式指定邮件头。

例如,使用 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();

希望有帮助。

如果你不喜欢使用外部类,可以按照PHP手册页中的例子。基本上,您只需要将 UTF-8 添加到您的邮件标头(而不是浏览器标头)中。

$headers = 数组    (        "MIME 版本:1.0",        '内容类型:文本/html;charset="UTF-8";',        "内容传输编码:7 位",        "日期:" 。日期('r', $_SERVER['REQUEST_TIME']),        "消息 ID:",        '从: ' .$from,        '回复:' 。$from,        "返回路径:"。$from,        "X-Mailer: PHP v" .phpversion(),        "X 源 IP:"。$_SERVER['SERVER_ADDR'],    );    邮件($to, '=?UTF-8?B?' .base64_encode($arr["主题"]) .'?=', $arr['message'], implode("'", $headers));
我认为

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

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

你的 HTML 必须有

<meta charset="utf-8">,当 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>

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

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

希望这对您有所帮助!

祝你好运麦克。