phphtmlformsspecial-characters

Accented/special characters not recognized by PHP mail form


As I see, it is an endlessly asked topic... I have read and reread a dozen posts with the same problem and I have not been able to find any solution to solve it in my case: So, for example, "Envío" is displayed as "Envío" in the message that I receive. The code I have is the following... and I have modified it according to many (many!) different suggestions that I have found, without getting any of them to work!

Please, help! =(

<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <link href="assets/css/style.css" rel="stylesheet">
  <link href="assets/css/custom.css" rel="stylesheet">
</head>
<body>

<div class="col-md-6">
<div class="form contact-form">
        <?php
        error_reporting(0);
        $action=$_REQUEST['action']; 
        if ($action=="")    /* display the contact form */ 
        { 
        ?>
<form class="php-email-form" id="contact-form" action="<?php $_SERVER["PHP_SELF"];?>" method="POST" enctype="multipart/form-data" autocomplete="etelpmocotua">
<input type="hidden" name="action" value="submit">
<div class="form-group">
<input class="form-control" type="text" name="name" value="" size="30" placeholder="Nombre" class="form-area" autocomplete="etelpmocotua" /></div>
<div class="form-group mt-3"><input class="form-control" type="text" name="email" value="" size="30" placeholder="e-Mail" class="form-area" autocomplete="etelpmocotua" /></div>
<div class="form-group mt-3"><input class="form-control" type="text" name="phone" value="" size="30" placeholder="Teléfono" class="form-area" autocomplete="etelpmocotua" /></div>
<textarea class="form-control mt-3" name="message" placeholder="Tu mensaje" class="form-area"></textarea>
<div class="my-3"><div class="loading">Enviando...</div></div>
<div class="text-left"><button type="submit">Enviar Mensaje</button></div>
</form>

        <?php 
                }  
        else
        {
        $name=$_REQUEST['name']; 
        $email=$_REQUEST['email'];
        $phone=$_REQUEST ['phone']; 
        $message=$_REQUEST['message'];

        if (($name=="")||($email=="")||($phone=="")||($message=="")) 
        { 
        echo "<div class='error-message'><strong>Tu mensaje no pudo ser enviado</strong><br>Por favor, completá todos los campos.<br>
        <a href=\"javascript:history.back()\" target=\"_self\"><img src='assets/img/back.png' /></a></div>";
        } 

        else{
        $body .="Nuevo mensaje desde la web!\n
        Nombre: $name
        e-Mail: $email
        Telefono: $phone\n
        Mensaje:\n
        $message";
        $from .="From: 'web@mydomain.com'>\r\n Return-path: $email";
        $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

        $subject .="Contacto WEB";
        mail("web@mydomain.com", $subject, $body, $from);

        echo "<div class='sent-message'>Tu mensaje ha sido enviado<br />
        y pronto será respondido.<br><br>
        <strong>Gracias por contactarnos!<strong></div>"; 
        }
        }
        ?>
</div>
</div>
</body>

Solution

  • There are a few points which can be fine-tuned, or needed to be corrected

    1. You tried specifying utf-8 in your $headers, but your $headers is not used in mail()
    2. You are using HTML format, but have tried using \n (better use <br>)
    3. If you have entered carriage return(s) in your message, it will not be displayed properly in HTML , so better use nl2br to parse it

    So, the revised code will be: (make sure you have replaced your_email@your_domain.com with your real email address to receive the mail)

    <html lang="es">
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
      <link href="assets/css/style.css" rel="stylesheet">
      <link href="assets/css/custom.css" rel="stylesheet">
    </head>
    <body>
    
    <div class="col-md-6">
    <div class="form contact-form">
    
    <?php
    
    $action=$_REQUEST['action']; 
    
    if ($action=="")  {  ?>
    
    <form class="php-email-form" id="contact-form" action="<?php $_SERVER["PHP_SELF"];?>" method="POST" enctype="multipart/form-data" autocomplete="etelpmocotua">
    <input type="hidden" name="action" value="submit">
    <div class="form-group">
    <input class="form-control" type="text" name="name" value="" size="30" placeholder="Nombre" class="form-area" autocomplete="etelpmocotua" /></div>
    <div class="form-group mt-3"><input class="form-control" type="text" name="email" value="" size="30" placeholder="e-Mail" class="form-area" autocomplete="etelpmocotua" /></div>
    <div class="form-group mt-3"><input class="form-control" type="text" name="phone" value="" size="30" placeholder="Teléfono" class="form-area" autocomplete="etelpmocotua" /></div>
    <textarea class="form-control mt-3" name="message" placeholder="Tu mensaje" class="form-area"></textarea>
    <div class="my-3"><div class="loading">Enviando...</div></div>
    <div class="text-left"><button type="submit">Enviar Mensaje</button></div>
    </form>
    
    <?php }  else   {
            $name=$_REQUEST['name']; 
            $email=$_REQUEST['email'];
            $phone=$_REQUEST ['phone']; 
            $message=$_REQUEST['message'];
    
            if (($name=="")||($email=="")||($phone=="")||($message=="")) 
            { 
            echo "<div class='error-message'><strong>Tu mensaje no pudo ser enviado</strong><br>Por favor, completá todos los campos.<br>
            <a href=\"javascript:history.back()\" target=\"_self\"><img src='assets/img/back.png' /></a></div>";
            } else{
            $body .="Nuevo mensaje desde la web!<br><br>
            Nombre: $name<br>
            e-Mail: $email<br>
            Telefono: $phone<br>Mensaje:<br><br>". nl2br($message);
            //    $headers .="From: 'web@mydomain.com'>\r\n Return-path: $email";
            //    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            $headers .= 'From: <'. $email . '>' . "\r\n";
            //$headers .= 'Cc: myboss@example.com' . "\r\n";
    
            $subject .="Contacto WEB";
            mail("your_email@your_domain.com", $subject, $body, $headers);
    
            echo "<div class='sent-message'>Tu mensaje ha sido enviado<br />
            y pronto será respondido.<br><br>
            <strong>Gracias por contactarnos!<strong></div>"; 
            }
         }
    ?>
    
    </div>
    </div>
    </body>
    

    Note: Envío (e.g name) will still be seen as Envío if you use the above code to send out an email