php - 使用 PHPMailer 在 HTML 电子邮件中不能很好地显示 UTF-8 口音
问题描述
我正在设置一个 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
口音“é”也显示有“é”。
我不知道问题出在哪里。
如果我的代码编程良好,有什么线索吗?
谢谢。
解决方案
代替
$mail -> charSet = "UTF-8";
和
$mail->CharSet = 'UTF-8';
推荐阅读
- python - Python Lambda 函数无法应用于多列
- python - 烧瓶 api 路由请求获取参数丢失错误
- pandas - 具有多个条件的 Pandas drop_duplicates
- multidimensional-array - 如何快速对多维数组进行排序
- node.js - 如何呈现特定选项卡
- css - 在图像上显示版权作为标题
- c++ - cmake/cpack 分发一个库及其依赖项
- javascript - node.js net.connect() 错误,尽管 try-catch
- powershell - 术语“Install-ChocolateyPackage”未被识别为 cmdlet、函数的名称
- python - 将列中的值移动到该列中每个空白行之前的一行