Question

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>'; 
}
}
    ?>
Was it helpful?

Solution

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" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top