How to enable UTF-8 character encoding in a php formmail

192 Views Asked by At

This is the code:

    <?php
if($_POST["message"]) {
    mail("[email protected]", "Note To Self", $_POST["message"], "From: [email protected]");
    header( "Location: sent.html" );
}
?>

English characters are working fine but Chinese, Japanese and Korean come out in the email like this:

ムコ 猫 ê³ ì–‘ì ´

Based on this topic: Charset UTF-8 not working on <?php contact form I added content type and character set:

<?php
if($_POST["message"]) {
    mail("[email protected]", "Note To Self", $_POST["message"], "From: [email protected]", "Content-Type: text/html; charset=UTF-8");
    header( "Location: sent.html" );
}
?>

but now I don't receive the emails at all. What am I doing wrong?

1

There are 1 best solutions below

0
On

I've got it working now. Multiple extra headers should be separated with a CRLF (\r\n).

<?php

$headers = 'From: [email protected]' . "\r\n";
$headers .= 'Content-Type: text/plain; charset=UTF-8' . "\r\n";

if($_POST["message"]) {
    mail( "[email protected]", "Note To Self", $_POST["message"], $headers );
    header( "Location: sent.html" );
}
?>