2016-11-14 26 views
0

我試圖用PHPMailer發送一個HTML郵件(和一個後備純文本文件)。我的問題是,當我收到電子郵件時,由於添加了換行符,所以HTML代碼變得一團糟。如何在使用PHPMailer作爲電子郵件發送時保留HTML代碼

我試過將編碼從quoted-printable更改爲8bit7bit,但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> 

回答

1

PHPMailer處理自動時間過長的行,例如,如果行數切換爲quoted-printable超過998個字符。但是,PHP mail()函數有時會嘗試使用較小的值執行相同的操作,從而導致使用雙重編碼的消息中斷。如果使用SMTP而不是mail()(PHPMailer的默認設置)發送郵件,則可能會有更多運氣 - 請撥打$mail->isSMTP();,並按照PHPMailer提供的示例設置合適的SMTP選項,即使它只是本地主機。

你也運行一個老版本的PHPMailer - 最新版本是5.2.16。

我還建議嘗試未發佈的PHPMailer 6.0 branch,因爲它會對換行處理進行一些重大更改(您可能需要稍微調整一下代碼)。

0

您可以手動指示PHPMailer的包長的線,內置的功能wrapText()

wrapText(string $message, integer $length, boolean $qp_mode = false):字符串

自動換行的消息。

用於不自動執行包裝的郵件程序和用於引用打印的編碼郵件的 。 參數

$message該消息包

整數$length的線路長度纏繞到

布爾$qp_mode是否在引用打印模式下運行

另外,不要忘記立即設置self::$Mailer->IsHTML(true)self::$Mailer->Body=$template['html'];

函數IsHTML(true)指示您正在發送HTML電子郵件的類。

相關問題