1

I have a contact form and I'm having a little bit of trouble with codificatio. I dont know why but the special characters (it's spanish) show badly in the email received. I'm a begginner in this so I dont really know where is the problem. My web is declared in UTF-8 and here is the code

<?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>'; 
}
}
    ?>
Elavarasan
  • 2,579
  • 2
  • 25
  • 42
Manuls
  • 27
  • 3

1 Answers1

0

Update header;

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

as

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

And state utf-8 thing in your message html header like;

< meta charset="utf-8" />
Hüseyin BABAL
  • 15,400
  • 4
  • 51
  • 73
  • More info about PHP_EOL : http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol – Whitebird Apr 04 '14 at 12:29
  • Kinda depends on the mail server you're using, Postfix prefers the line-ending to be in the format native to the OS, so `PHP_EOL` would actually be the right choice for mail headers. – CD001 Apr 04 '14 at 12:40
  • @CD001 `"\r\n"` is just for the standard. Main case here convert `text/html` to `text/plain` – Hüseyin BABAL Apr 04 '14 at 12:42