Charset UTF-8无法处理<! - ?php联系表单 - >

时间:2013-09-12 13:21:52

标签: php html5 utf-8

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

我尝试使用“标题('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");
    ?>

7 个答案:

答案 0 :(得分:2)

您没有在那里设置邮件标头,而是设置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);
?>

参考:

答案 1 :(得分:1)

以下是我的案例(塞尔维亚语,克罗地亚语,波斯尼亚语......):

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

答案 2 :(得分:0)

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

http://swiftmailer.org/

答案 3 :(得分:0)

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

希望有所帮助。

答案 4 :(得分:0)

如果您不想使用外部课程,可以按照PHP manual page中的示例进行操作。基本上,您只需要将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));

答案 5 :(得分:0)

我认为这不是电子邮件的问题,是$_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>

答案 6 :(得分:0)

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

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

?>

希望这会帮助你!

祝你好运, MIKEY。