【发布时间】:2022-01-29 22:54:35
【问题描述】:
我正在通过 pear 发送电子邮件,但最终结果有时会损坏字符。尽管电子邮件大致相同(几个词更改,收件人电子邮件地址),但损坏的字符并不总是在 html 正文的同一位置。你能看到一些奇怪的东西吗?那里的字符。邮件通过php PEAR发送如下:
通过 gmail 帐户收到的邮件正文已损坏的示例。有时链接会损坏(它会添加一个空格和/或 x0D / %0D 字符 - 这是回车)。每次,只有一个词被破坏,如下所示。有时被破坏的角色出现在主题中,但很少见。
Received: from localhost (some.domain.com [1.2.3.4]) (Authenticated sender: xxx@mydomain.com) by blabla.domain.com (ESMTP) with ESMTPSA for <recipient@gmail.com>; Tue, 25 Jan 2022 01:06:05 +0200 (EET)
From: xxx@mydomain.com
To: recipient@gmail.com
Date: Tue, 25 Jan 2022 01:06:04 +0200
Content-Type: text/html; charset=UTF-8
X-Mailer: PHP/7.x.x
Reply-To: xxx@mydomain.com
Subject: my subject
<html><head><meta http-equiv="Content-Language" content="el"/><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>...Content in greek here... όμορφος, πα�
�άτε εκεί ... content here</body></html>
而不是“πατάτε”这个词
php_script_sending_mail.php
$UserMesssage = "some greek content here which gets corrupted at usually one word";
$MAILto = $varEmail;
$MAILmessage = "<html><head><meta http-equiv=\"Content-Language\" content=\"el\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
$MAILmessage .= $UserMesssage;
$MAILmessage .= "</body></html>";
mail_new($MAILto, $MAILsubject, $MAILmessage);
fn_mail.php
function mail_new($to, $subject, $message) {
require_once "Mail.php";
$from = "xxx@mydomain.com";
$host = "mailgate.isp.com";
$username = "xxx@mydomain.com";
$password = "pass";
$headers = array ('From' => $from, 'To' => $to, 'Date' => date('r', time()), 'Content-Type' => 'text/html; charset=UTF-8', 'X-Mailer' => 'PHP/'.phpversion(), 'Reply-To' => 'xxx@mydomain.com', 'Subject' => '=?UTF-8?B?'.base64_encode($subject).'?=');
$smtp = @Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = @$smtp->send($to, $headers, $message);
}
两个文件都保存为没有 BOM 的 UTF-8。
我做错了吗?我该如何解决这个问题? 提前谢谢你。
【问题讨论】:
-
您还需要考虑这些: 1. 在标题中添加
MIME-Version: 1.0以支持不同类型的字符和非文本数据。 2.另一种编码,如charset=iso-8859-1 -
mime 版本标头不能解决问题。我不能使用其他字符集,我需要 utf8
-
只是为了将来,当你说单独使用
pear时,它没有任何意义,pear 只是 PHP 的扩展框架,你应该总是有一个 pear 扩展,我也想说你想要的很少现在发送邮件的服务器垃圾邮件过滤器很麻烦,你最好使用像mailgun这样的外部服务,因为它们会告诉你一切都是正确的,并且来自半白名单的邮件服务器