PHP联系表格邮件坏编码

时间:2014-04-04 12:23:25

标签: php email contact-form

我有一份联系表格,我在编纂方面遇到了一些麻烦。我不知道为什么,但特殊字符(它的西班牙语)在收到的电子邮件中显示得很糟糕。我是这方面的一个前任,所以我真的不知道问题出在哪里。我的网站以UTF-8声明,这是代码

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message =  $_POST['message'];
$from = 'From: web.com'; 
$to = 'email@email.com'; 
$subject = 'Formulario contacto web';
$body = "Nombre: $name<br> E-Mail: $email<br> Mensaje:<br> $message";
$headers  = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-Type: text/html; charset=utf-8" . PHP_EOL;
$headers .= "From: " . $from . "\n";

if ($_POST['submit']) {
if (mail($to, $subject, $body, $headers)) { 
    echo '<script type="text/javascript">alert("Su mensaje ha sido enviado");</script>';
} else { 
    echo '<script type="text/javascript">alert("Algo ha ido mal. Inténtelo de nuevo por favor");</script>'; 
}
}
    ?>

1 个答案:

答案 0 :(得分:0)

更新标题;

$headers .= "Content-Type: text/html; charset=utf-8" . PHP_EOL;

作为

$headers .= 'Content-Type: text/plain; charset=utf-8' . "\r\n";

并在你的消息html标题中声明utf-8的内容,如;

< meta charset="utf-8" />