简体   繁体   中英

Charset UTF-8 not working on <?php contact form

I am trying to make utf-8 work when receives the email. When the form is filled up the characters shows as codes if is ç/Ç shows &$ Ex: Avan ç ado shows Avan &$ ado

I Tried using the " header('Content-Type: text/html;charset=UTF-8'); " but still not working please help me thank you so much...

This is my code...

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

You aren't setting the mail header there, you are setting the http header. This function header is sending a raw HTTP header, it isn't doing anything for the email you are sending

   header('Content-Type: text/html;charset=UTF-8');

You need to add the header "Content-Type: text/html; charset=UTF-8" (for HTML Email bodies) or "Content-Type: text/plain; charset=UTF-8" (for Plain Text Email bodies) to your mail function. Like this.

$headers = array("Content-Type: text/html; charset=UTF-8");
mail($to, $subject, $message, $headers)

Additionally, for email, each lines should be separated with a CRLF (\\r\\n) instead of merely using a linefeed (\\n). A fully example end result might look more so like this:

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

Reference:

Here is how it work in my case (Serbian, Croatian, Bosnian, ...):

in HTML page: In header of yours email-form.html:

<meta http-equiv="Content-Type" content="text/html; charset=windows-UTF-8">

and in the "form":

<form name="contactform" method="post" action="http://yourdomain.com/e-mail.php" accept-charset='UTF-8' >    

in PHP-mailer:

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

I would recommend usging a framework like Swift Mailer to send the email messages, instead of creating your own email headers. That might solve the charset problem as well.

http://swiftmailer.org/

The header() function is for sending HTTP headers to the web browser that is loading the page. It does not have any effect at all on email content send via mail() .

In order to send mail headers using mail() you need to build them manually in the correct format.

This can be tricky to get right, and can make for very messy code, so I would suggest that instead you consider using a decent mailer class such as PHPMailer or Swiftmailer , either of which will allow you to specify mail headers in a much simpler way.

With PHPMailer, for example, you code could look something like this:

$mail = new PHPMailer();
$mail->From = $from;
$mail->AddAddress($to);
$mail->Body = "......";
$mail->Subject = "....";
$mail->CharSet="UTF-8";    //see, very easy in phpMailer!
$mail->Send();

Hope that helps.

If you won't like to use external class, may follow example in PHP manual page . Basically, you just need to add UTF-8 to your mail header (not browser header).

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

I think that is not a problem with the email, is a problem with the string characters contained in the $_POST variables!

When you send those strings from the HTML form to your PHP they are not encoded with UTF8.

Your HTML must have <meta charset="utf-8"> and when the HTML renders from the PHP, that PHP must have header('Content-Type: text/html;charset=UTF-8'); .

Like this:

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

Try this syntax for your fields. I had the exact same issue when I was trying to send French accents:

$variable = utf8_encode(htmlentities($_POST['variable'], ENT_QUOTES, "UTF-8"));

The above takes the form field and places it into a variable encoded correctly as UTF-8.

For your example, I have changed it accordingly, so give this a go:

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

?>

Hope this helps you out!

Good luck, Mikey.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM