繁体   English   中英

Charset UTF-8无法正常工作

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

收到电子邮件时,我正在尝试使utf-8工作。 当窗体被填满的字符显示为代码,如果是C / C显示&$例:爱王çADO显示爱王&$ 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");
    ?>

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

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

您需要添加标题“ Content-Type:文本/ html; charset = UTF-8” (对于HTML电子邮件正文)或“ Content-Type:文本/纯文本; 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);
?>

参考:

在我的情况下(塞尔维亚文,克罗地亚文,波斯尼亚文...),这是这样的:

在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-mailer中:

// 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()发送邮件头,您需要以正确的格式手动构建它们。

这样做可能很棘手,并且可能会使代码非常混乱,因此我建议您考虑使用像PHPMailerSwiftmailer这样的体面的Mailer类 ,这两种类都可以使您以更简单的方式指定邮件头。

例如,使用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    = array
    (
        'MIME-Version: 1.0',
        'Content-Type: text/html; charset="UTF-8";',
        'Content-Transfer-Encoding: 7bit',
        'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
        'Message-ID: ',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
        'X-Mailer: PHP v' . phpversion(),
        'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
    );

    mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $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"));

上面的代码使用form字段并将其放入正确编码为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");

?>

希望这可以帮助你!

祝你好运,Mikey。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM