我試圖用PHPMailer
發送一個HTML郵件(和一個後備純文本文件)。我的問題是,當我收到電子郵件時,由於添加了換行符,所以HTML代碼變得一團糟。如何在使用PHPMailer作爲電子郵件發送時保留HTML代碼
我試過將編碼從quoted-printable
更改爲8bit
或7bit
,但PHPMailer只是簡單地每次都恢復爲quoted-printable
。
我設法找到了這個在這個答案的原因:https://stackoverflow.com/a/33102397/2691879
基本上,它看起來像,即每增加998個字符後是一個換行符,是發送電子郵件時必須的,但我問題是,我怎麼可以放心,HTML代碼在發送電子郵件時被保留了呢?
我在這裏錯過了什麼嗎?
不幸的是,將換行手動添加到我使用的模板中是不可能的,因爲此代碼的預期用途是該網站的客戶端將能夠創建自己的模板以進行發送,而且,模板中還有一些可變數據,在發送之前get被取代。
下面是一些代碼:
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: <[email protected]>
Delivered-To: [email protected]
Received: from xxx.xxx.xxx
by xxx.xxx.xxx (Dovecot) with LMTP id wT5IIgnOKVh8fgAAhsLUGA
for <[email protected]>; Mon, 14 Nov 2016 14:45:29 +0000
Return-path: <[email protected]>
Envelope-to: [email protected]
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 <[email protected]>)
id 1c6IVl-0008M6-Bo
for [email protected]; 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: [email protected]
Subject: Account confirmation
X-PHP-Originating-Script: 505:PHPMailer.php
Date: Mon, 14 Nov 2016 16:44:49 +0200
From: PortalName - Account Information <[email protected]>
Message-ID: <[email protected]>
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.=
[email protected] </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>