php และ utf-8 SOS

ฉันมีปัญหากับการตั้งค่าเป็น utf-8 ใน php ด้วยแบบฟอร์มติดต่อนี้:

    <?php header('Content-type: text/plain; charset=utf-8');

require 'PHPMailer-master/PHPMailerAutoload.php';

$fromEmail = '[email protected]';
$fromName = 'test form';

$sendToEmail = '[email protected]';
$sendToName = 'TEST';

$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');

$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
    $emailTextHtml .= "<table>";

    foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
        }
    }
    $emailTextHtml .= "</table><hr>";
    $emailTextHtml .= "<p>Have a nice day,<br>Best,<br>Ondrej</p>";

    $mail = new PHPMailer;

    $mail->setFrom($fromEmail, $fromName);
    $mail->addAddress($sendToEmail, $sendToName);
    $mail->addReplyTo($from);

    $mail->isHTML(true);

    $mail->Subject = $subject;
    $mail->msgHTML($emailTextHtml);


    if(!$mail->send()) {
        throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
    }

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}


if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}

เมื่อฉันส่งข้อความ ฉันได้รับ: 'Сандñ€Ð ธна' ฉันอ่านฟอรัมและผลลัพธ์ของ Google ทั้งหมดแล้ว แต่ฉันไม่เข้าใจ ฉันพยายามเพิ่ม "header('Content-type: text/plain; charset=utf-8');" เกือบทุกที่แต่ก็ไม่ได้ช่วยอะไร

กรุณาช่วย :)


person ss777    schedule 15.12.2017    source แหล่งที่มา
comment
อ่าน stackoverflow.com/questions /279170/   -  person Jeff    schedule 15.12.2017
comment
ปัญหาที่เป็นไปได้ 2 ประการที่นี่: คุณส่งบางสิ่ง (นั่นคือช่องว่างบางส่วน) ก่อน <? ตามที่ดูเหมือนที่นี่ (อาจเป็นปัญหาการคัดลอกและวาง) บางทีไฟล์ php เองอาจไม่ได้เข้ารหัสเป็น utf-8   -  person Jeff    schedule 15.12.2017
comment
คุณต้องชี้แจงหากคุณได้รับข้อความที่อ่านไม่ออกในการตอบกลับ HTML ที่เพจของคุณสร้างขึ้น หรือในอีเมลที่คุณส่ง หรือทั้งสองอย่าง มันเป็นสองปัญหาที่แยกจากกันและมีแนวทางแก้ไขที่แตกต่างกัน   -  person Sammitch    schedule 15.12.2017
comment
มันอยู่ในข้อความอีเมล   -  person ss777    schedule 16.12.2017
comment
จากนั้นการตั้งค่าส่วนหัว HTTP จะไม่ช่วยแก้ปัญหา คุณต้องกำหนดค่า PHPMailer เพื่อตั้งค่าการเข้ารหัส UTF8 บนข้อความ $mail->CharSet = 'UTF-8'; stackoverflow.com/questions/2491475/   -  person Sammitch    schedule 16.12.2017
comment
มันได้ผล ขอบคุณมาก. :)   -  person ss777    schedule 16.12.2017