PHP fails to send UTF-8 Messages

php html html5 unicode

100 观看

1回复

72 作者的声誉

This question already has an answer here:

I have the following PHP code:

<?php

$to      = 'info@neatgr.cf';
$subject = date("d/m/Y");
$message = 'Hey 123 !@# αβγ';

$headers  = "From: testsite < mail@testsite.com >\n";
$headers .= "Cc: testsite < mail@testsite.com >\n"; 
$headers .= "X-Sender: testsite < mail@testsite.com >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: mail@testsite.com\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";

mail($to,$subject,$message,$headers);

echo "<script type='text/javascript'>alert('Message sent! We will get back to you soon!');</script>";
echo "<script>window.location.href = 'http://example.com';</script>";
?>

The mail gets sent fine. The problem is that αβγ (Unicode Characters) doesn't get on the mail recipient's end correctly.

This is what the recipient sees: Hey 123 !@# αβγ This is what he should see: Hey 123 !@# αβγ

I've looked everywhere and tried everything, changing headers, converting my string to unicode etc. etc., but nothing has worked. Maybe I'm doing something wrong?

作者: Coto TheArcher 的来源 发布者: 2017 年 12 月 27 日

回应 1


4

2397 作者的声誉

决定

Use the UTF-8 charset header, for example:

$headers .= "Content-Type: text/html; charset=iso-8859-1\n";

Goes to:

$headers .= "Content-Type: text/html; charset=UTF-8\n";
作者: samdd 发布者: 2017 年 12 月 27 日
32x32