使用PHPMailer在HTML电子邮件中无法正确显示UTF-8口音

时间:2019-06-18 14:20:47

标签: php utf-8 phpmailer

我正在设置一个PHPMailer PHP表单,该表单会将我们发送给我们的Office 365帐户。我对显示的法国口音有疑问,像“ééààçç”这样的口音带有“ééÃÃÃÃÃç秔。

PHP表单以UTF-8编码; PHP代码也以UTF-8编码;

但是收到的电子邮件似乎没有显示正确的字符。

我已经添加了这些设置,但没有任何变化:

在PHP文件中

header('Content-Type: charset=utf-8');

$mail->isHTML(true);     // Set email format to HTML
$mail->CharSet = "UTF-8";

Php发送表单源代码:

<?php
header('Content-Type: charset=utf-8');
ini_set('startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(E_ALL);    

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'php/phpmailer/vendor/phpmailer/phpmailer/src/Exception.php';
require 'php/phpmailer/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'php/phpmailer/vendor/phpmailer/phpmailer/src/SMTP.php';

$nom_compagnie = $_POST['nom_compagnie'];
$nom_complet = $_POST['nom_complet'];
$poste = $_POST['poste'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$commentaire = $_POST['commentaire'];
$from = $_POST['email'];

function post_captcha($user_response) {
    $fields_string = '';
    $fields = array(
        'secret' => 'PrivateKey',
        'response' => $user_response
    );
    foreach($fields as $key=>$value)
    $fields_string .= $key . '=' . $value . '&';
    $fields_string = rtrim($fields_string, '&');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);

    $result = curl_exec($ch);
    curl_close($ch);

    return json_decode($result, true);
}
    $res = post_captcha($_POST['g-recaptcha-response']);

if (!$res['success']) {
    // What happens when the reCAPTCHA is not properly set up
    echo 'reCAPTCHA error: Check to make sure your keys match the registered domain and are in the correct locations. You may also want to doublecheck your code for typos or syntax errors.';

} else {
    // If CAPTCHA is successful...
    try {
    $mail = new PHPMailer(true);
    $mail->isSMTP();
    $mail->Host = 'smtp.office365.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = 'EmailAccount';
    $mail->Password = 'Password';
    $mail->addReplyTo($from, $nom_complet);
    $mail->SetFrom ("Hidden", "Hidden");
    $mail->addCC ("Hidden", "Hidden");
    $mail->addAddress ('Hidden', 'Hidden);
    //$mail->SMTPDebug = 3;
    //$mail->Debutoutput = fonction($str, $level) {echo "debug level $level; message: $str";}; //

    $mail->isHTML(true);     // Set email format to HTML
    $mail->Subject = "FORMULAIRE CONTACT";
    $mail->Body    = "
    <html>
    <head>
    <meta charset=\"UTF-8\">
    <title>Formulaire de Contact</title>
    ....
    </html>";
    $mail->AltBody = "";
    $mail->CharSet = "UTF-8";

    //$mail->msgHTML(file_get_contents('email_form_contact-fr.html'));
    $mail->send();
    // Paste mail function or whatever else you want to happen here!
    } catch (Exception $e) {
        echo $e->getMessage();
        die(0);
    }
        header('Location: envoi_form_contact-success-fr.html');
}

?>

收到的电子邮件显示如下:

显示电子邮件中的H3标题

Vous avez reçu un formulaire de contact via le site Web

应该这样写

Vous avez reçu un formulaire de contact via le site web

重音“é”也显示为“é”。

我不知道问题出在哪里。

任何提示我的代码是否经过良好编程?

谢谢。

2 个答案:

答案 0 :(得分:1)

替换

$mail -> charSet = "UTF-8"; 

$mail->CharSet = 'UTF-8'; 

答案 1 :(得分:0)

BonjourStéphane。 This page描述您所看到的症状;一个字符变成两个字符意味着您的数据使用UTF-8,但使用8位字符集显示。一般来说,PHPMailer会正确地做到这一点,因此您需要弄清楚哪里出了问题。

如果您使用collectionView.showsVerticalScrollIndicator = false ,您将能够看到正在发送的消息(使用像SMTPDebug = 2这样的非常简短的消息正文,该消息正文保证在UTF- 8)。

请确保脚本文件本身的编码也为UTF-8-将表情符号放入其中是确保其正确的好方法。

诊断此问题的原因是,您会发现操作系统受到干扰-复制到剪贴板等操作很可能会更改编码-因此处理该问题的方法是使用十六进制转储功能,该功能可让您检查实际字节值。法语是比较容易看的一种,因为几乎所有字符都是常规的单字节ASCII兼容字符,并且带重音符号的字符也更容易发现。在PHP中,bin2hex()函数将执行此操作。

您有2种错别字:é?应该是Debutoutput,而Debugoutput应该是fonction-这是从以下位置转储十六进制数据的好地方:

function

这是一个例子:

$mail->Debugoutput = function($str, $level) {echo $str, "\n", bin2hex($str), "\n";};

将产生echo 'abc ', bin2hex('abc'); ;每个输入字符都会输出2个十六进制数字(1个字节)。如果您输入的是abc 616263,而字符集是ISO-8859-1,则它将以abé的形式出现,因为该字符集中的é是abé 6162e9(十六进制)。 UTF-8中的相同字符串将以e9的形式出现,因为UTF8中的é是abé 6162c3a9-两个字节,而不仅仅是一个字节。像这样检查字符可以使您完全确定数据所在的字符集-仅查看它是不够的!

因此,尽管我不能确切地说出您的问题出在哪里,但希望您对如何诊断出一些更好的想法。