我正在尝试使用PHPMailer
发送HTML邮件(以及后备纯文本)。我的问题是,当我收到电子邮件时,HTML代码会因为添加到其中的换行符而全部被取消。
我尝试将编码从quoted-printable
更改为8bit
或7bit
,但PHPMailer每次只是简单地恢复为quoted-printable
。
我在这个答案中找到了原因:https://stackoverflow.com/a/33102397/2691879
基本上,看起来,在发送电子邮件时,每998个字符后添加换行符是必须的,但我的问题是,如何确保在发送电子邮件时保留HTML代码然后?
我在这里错过了什么吗?
不幸的是,手动将换行符添加到我使用的模板中是不可能的,因为此代码的预期用途是该站点的客户端能够创建自己的模板以进行发送,并且另外,模板中有一些可变数据,在发送之前会被替换。
以下是一些代码:
self::$Mailer->addAddress($to);
self::$Mailer->Subject=$subject;
self::$Mailer->Body=$template['html'];
self::$Mailer->AltBody=$template['text'];
self::$Mailer->CharSet = 'UTF-8';
self::$Mailer->Encoding = '7bit';
self::$Mailer->send();
我使用的HTML模板如下:
<h2> Thank you for joining %PORTALNAME <h2><span style='font-size:14px; font-weight: normal;'> You are receiving this e-mail because you (hopefully) created a new member account at %PORTALNAME! In order to be able to fully use your account, you will need to confirm your e-mail address, by clicking on %CONFIRMATIONLINK.<br> If your e-mail client doesn't recognize the above link, you can copy this url <b>%JUSTLINK</b> and paste it in any browser window, to confirm your account. <br><br> If it wasn't you, who opted for this registration, please ignore this message!</span> <br> <h3 style='margin-bottom: 5px;'> Your login credentials: </h3><div style='width: 100px; float: left;'> Username: </div><div style='float: left;'> %EMAIL </div><div style='clear:both;'></div><div style='width: 100px; float: left;'> Password: </div><div style='float: left;'> %PLAINPASSWORD </div><div style='clear:both;'></div>
我收到的消息来源如下:
Return-Path: <xxx@xxx.xxx>
Delivered-To: xxx@xxx.xxx
Received: from xxx.xxx.xxx
by xxx.xxx.xxx (Dovecot) with LMTP id wT5IIgnOKVh8fgAAhsLUGA
for <xxx@xxx.xxx>; Mon, 14 Nov 2016 14:45:29 +0000
Return-path: <xxx@xxx.xxx>
Envelope-to: xxx@xxx.xxx
Delivery-date: Mon, 14 Nov 2016 14:45:29 +0000
Received: from xxx.xxx.xxx ([199.191.58.218]:48854)
by xxx.xxx.xxx with esmtp (Exim 4.87)
(envelope-from <xxx@xxx.xxx>)
id 1c6IVl-0008M6-Bo
for xxx@xxx.xxx; Mon, 14 Nov 2016 14:45:29 +0000
Received: by xxx.xxx.xxx (Postfix, from userid 48)
id 7071DAA810F7; Mon, 14 Nov 2016 09:44:49 -0500 (EST)
To: xxx@xxx.xxx
Subject: Account confirmation
X-PHP-Originating-Script: 505:PHPMailer.php
Date: Mon, 14 Nov 2016 16:44:49 +0200
From: PortalName - Account Information <xxx@xxx.xxx>
Message-ID: <f2eb3096b55450ad7725097793b72ba9@PortalName.agentventure.com>
X-Mailer: PHPMailer 5.2.14 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<h2> Thank you for joining PortalName <h2><span style=3D'font-size:14px; font-w=
eight: normal;'> You are receiving this e-mail because you (hopefully) crea=
ted a new member account at PortalName! In order to be able to fully use your a=
ccount, you will need to confirm your e-mail address, by clicking on <a hre=
f=3D'xxx.xxx.xxx/verify-account/xxxxxx/'> this link</a>.<br> I=
f your e-mail client doesn't recognize the above link, you can copy this ur=
l <b>xxx.xxx.xxx/verify-account/xxxxxx/</b> and paste it in an=
y browser window, to confirm your account. <br><br> If it wasn't you, who o=
pted for this registration, please ignore this message!</span> <br> <h3 sty=
le=3D'margin-bottom: 5px;'> Your login credentials: </h3><div style=3D'widt=
h: 100px; float: left;'> Username: </div><div style=3D'float: left;'> xxx.=
xxx@xxx.xxx </div><div style=3D'clear:both;'></div><div style=
=3D'width: 100px; float: left;'> Password: </div><div style=3D'float: left;=
'> xxxxxxxxx </div><div style=3D'clear:both;'></div>
答案 0 :(得分:1)
PHPMailer处理自动过长的行,例如,如果行超过998个字符,则切换为quoted-printable。但是,PHP mail()函数有时会尝试使用较小的值执行相同的操作,从而导致使用双重编码的邮件损坏。如果您使用SMTP而不是mail()
(PHPMailer的默认值)发送,您可能会有更多的运气 - 调用$mail->isSMTP();
并设置适当的SMTP选项,如PHPMailer提供的示例中所示,即使它只是localhost。
您还运行旧版本的PHPMailer - 最新版本为5.2.16。
我还建议您尝试使用未发布的PHPMailer 6.0 branch,因为它会对换行处理进行一些重大更改(您可能需要稍微调整一下代码)。
答案 1 :(得分:0)
您可以手动指示PHPMailer使用内置函数wrapText()
包装长行:
wrapText(string $message, integer $length, boolean $qp_mode = false)
:string自动换行信息。
用于不自动执行包装的邮件程序 用于引用可打印的编码消息。 参数
string
$message
要包装的消息integer
$length
要换行的行长度boolean
$qp_mode
是否以Quoted-Printable模式运行
此外,不要忘记在self::$Mailer->IsHTML(true)
之后立即设置self::$Mailer->Body=$template['html'];
函数IsHTML(true)
指示您发送HTML电子邮件的类。