简体   繁体   English

Charset UTF-8无法正常工作

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

I am trying to make utf-8 work when receives the email. 收到电子邮件时,我正在尝试使utf-8工作。 When the form is filled up the characters shows as codes if is ç/Ç shows &$ Ex: Avan ç ado shows Avan &$ ado 当窗体被填满的字符显示为代码,如果是C / C显示&$例:爱王çADO显示爱王&$ ADO

I Tried using the " header('Content-Type: text/html;charset=UTF-8'); " but still not working please help me thank you so much... 我尝试使用“ header('Content-Type:text / html; charset = UTF-8');”,但仍无法正常工作,请非常感谢我...

This is my code... 这是我的代码...

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

You aren't setting the mail header there, you are setting the http header. 您没有在此处设置邮件头,而是在设置http头。 This function header is sending a raw HTTP header, it isn't doing anything for the email you are sending 此函数header发送的是原始HTTP标头,它对您发送的电子邮件没有任何作用

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

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. 您需要添加标题“ Content-Type:文本/ html; charset = UTF-8” (对于HTML电子邮件正文)或“ Content-Type:文本/纯文本; charset = UTF-8” (对于纯文本电子邮件正文)到您的mail功能。 Like this. 像这样。

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

Additionally, for email, each lines should be separated with a CRLF (\\r\\n) instead of merely using a linefeed (\\n). 此外,对于电子邮件,每行应使用CRLF(\\ r \\ n)分隔,而不是仅使用换行(\\ 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);
?>

Reference: 参考:

Here is how it work in my case (Serbian, Croatian, Bosnian, ...): 在我的情况下(塞尔维亚文,克罗地亚文,波斯尼亚文...),这是这样的:

in HTML page: In header of yours email-form.html: 在HTML页面中:在您的email-form.html标头中:

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

and in the "form": 并在“表单”中:

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

in PHP-mailer: 在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);?>    

I would recommend usging a framework like Swift Mailer to send the email messages, instead of creating your own email headers. 我建议使用像Swift Mailer这样的框架来发送电子邮件,而不是创建自己的电子邮件标题。 That might solve the charset problem as well. 那也可以解决字符集问题。

http://swiftmailer.org/ http://swiftmailer.org/

The header() function is for sending HTTP headers to the web browser that is loading the page. header()函数用于将HTTP标头发送到正在加载页面的Web浏览器。 It does not have any effect at all on email content send via mail() . 它对通过mail()发送的电子邮件内容完全没有任何影响。

In order to send mail headers using mail() you need to build them manually in the correct format. 为了使用mail()发送邮件头,您需要以正确的格式手动构建它们。

This can be tricky to get right, and can make for very messy code, so I would suggest that instead you consider using a decent mailer class such as PHPMailer or Swiftmailer , either of which will allow you to specify mail headers in a much simpler way. 这样做可能很棘手,并且可能会使代码非常混乱,因此我建议您考虑使用像PHPMailerSwiftmailer这样的体面的Mailer类 ,这两种类都可以使您以更简单的方式指定邮件头。

With PHPMailer, for example, you code could look something like this: 例如,使用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();

Hope that helps. 希望能有所帮助。

If you won't like to use external class, may follow example in PHP manual page . 如果您不想使用外部类,请遵循PHP手册页中的示例。 Basically, you just need to add UTF-8 to your mail header (not browser header). 基本上,您只需要在邮件标题(而不是浏览器标题)中添加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));

I think that is not a problem with the email, is a problem with the string characters contained in the $_POST variables! 我认为这不是电子邮件的问题,而是$_POST变量中包含的字符串字符的问题!

When you send those strings from the HTML form to your PHP they are not encoded with UTF8. 当您将这些字符串从HTML表单发送到PHP时,它们不会使用UTF8进行编码。

Your HTML must have <meta charset="utf-8"> and when the HTML renders from the PHP, that PHP must have header('Content-Type: text/html;charset=UTF-8'); 您的HTML必须具有<meta charset="utf-8">并且当HTML从PHP呈现时,该PHP必须具有header('Content-Type: text/html;charset=UTF-8'); .

Like this: 像这样:

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

Try this syntax for your fields. 尝试为您的字段使用此语法。 I had the exact same issue when I was trying to send French accents: 当我尝试发送法语口音时,我遇到了完全相同的问题:

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

The above takes the form field and places it into a variable encoded correctly as UTF-8. 上面的代码使用form字段并将其放入正确编码为UTF-8的变量中。

For your example, I have changed it accordingly, so give this a go: 对于您的示例,我已经对其进行了相应的更改,因此可以尝试一下:

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

?>

Hope this helps you out! 希望这可以帮助你!

Good luck, Mikey. 祝你好运,Mikey。

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

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