使用PHPMailer发送电子邮件正文中出现感叹号标记

7

我在我的网站中使用phpmailer发送电子邮件。我的代码运行良好,但有时电子邮件消息正文中会随机出现感叹号。我的代码如下:

$mail->SetFrom(FROM_EMAIL,FROM_NAME); //emailid of sender(admin)                
$mail->Subject = 'Subject here.'; //subject of email
$mail->AddAddress(Address here); //emailid of user(recipient)
$content = 'some html code here';

$mail->MsgHTML($content); //this is body of email
$mail->Send();

这个可以正常运行,但有时候会出现感叹号,不知道为什么。提前感谢...

感叹号 ! 或问号 ? ?? 如果电子邮件客户端错误解读了字符集(因为您的消息错误地报告了它),则通常会出现无效字符作为 ? - Michael Berkowski
可能需要 $mail->CharSet = 'UTF-8'; - Michael Berkowski
4个回答

9
我认为这是因为电子邮件消息在一行中不能超过998个字符。请尝试添加以下内容:
$mail->WordWrap = 50;

1
请查看此链接:http://www.tinyfrogsoftware.com/the-story-of-the-rogue-exclamation-mark-or-how-i-lost-4-hours-of-my-life/希望对您有所帮助。 - Muthu Kumaran
3
可以尝试在“$content = 'some html code here';”后添加“$content = wordwrap($content, 50);”,这将对内容进行换行处理,每行最多包含50个字符。 - Muthu Kumaran
你能解释一下为什么会这样或者提供一个不能处理超过998个字符的参考吗? - Stefan Dunn
@StefanDunn,998个字符的限制是由于许多实现发送、接收或存储Internet消息格式消息的限制,这些消息在一行上不能处理超过998个字符。更多详情请参考RFC 2822 - Muthu Kumaran
Tinyfrogsoftware的链接已经失效。这里有一个存档版本 - https://web.archive.org/web/20100424013747/http://www.tinyfrogsoftware.com/the-story-of-the-rogue-exclamation-mark-or-how-i-lost-4-hours-of-my-life/ - Merlyn Morgan-Graham

4
我知道现在有点晚了,但是我有一个替代方案可以解决这个问题:
使用以下代码将整个消息使用base64进行编码:
$message = chunk_split(base64_encode($message));

然后,在您的标头中添加以下内容:
$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";

这将告诉邮件客户端您的消息是base64编码的。


2
如果您正在使用PHPmailer,则只需要一行代码即可:
$mail = new PHPMailer();
$mail->Encoding = 'base64';

这将在内部执行 Content-Transfer-Encoding: base64 和 chunk_split(base64_encode($message))。


0

我也遇到了这个问题,经过长时间的搜索,我发现你应该对你的HTML进行换行处理

$emailContent = '<p>some large html</p>';
$mail->msgHTML(wordwrap($emailContent, 50));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接