2014-07-16 50 views
0

我试图创建一个电子邮件,它是使用PHP的文本/纯文本和文本/ HTML格式。试图在PHP中创建一个文本/ HTML电子邮件

我想以此为榜样: Sending an email with php plain text and html

当我打开该消息是空白。我正在使用PHP mail()函数。

这是被创建的电子邮件源:在使用邮件功能Content-Transfer-Encoding: 7bit

From - Wed Jul 16 15:54:24 2014 
X-Account-Key: account2 
X-UIDL: UID17453-1394824486 
X-Mozilla-Status: 0001 
X-Mozilla-Status2: 00000000 
X-Mozilla-Keys:                     
Return-path: <[email protected]> 
Envelope-to: [email protected] 
Delivery-date: Wed, 16 Jul 2014 15:55:11 -0400 
Received: from vps.corporations.org ([69.73.133.204]:35077) 
    by vortex.nocdirect.com with esmtps (TLSv1:DHE-RSA-AES256-SHA:256) 
    (Exim 4.82) 
    (envelope-from <[email protected]>) 
    id 1X7VIA-002zYn-7q 
    for [email protected]; Wed, 16 Jul 2014 15:55:11 -0400 
Received: from energy by vps.corporations.org with local (Exim 4.82) 
    (envelope-from <[email protected]>) 
    id 1X7VI9-00022J-Rw 
    for [email protected]; Wed, 16 Jul 2014 15:55:05 -0400 
To: [email protected] 
Subject: New Test Subject -- [email protected] 
X-PHP-Script: www.energyjustice.net/action/thanks.php for 68.57.17.14 
From: d d <[email protected]> 
Reply-To: [email protected] 
X-Mailer: PHP/5.2.9 
MIME-Version: 1.0 
Content-Type: multipart/alternative; boundary="1kf9a19f031518xd151gkd" 
Message-Id: <[email protected]> 
Date: Wed, 16 Jul 2014 15:55:05 -0400 
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report 
X-AntiAbuse: Primary Hostname - vps.corporations.org 
X-AntiAbuse: Original Domain - campusactivism.org 
X-AntiAbuse: Originator/Caller UID/GID - [513 513]/[47 12] 
X-AntiAbuse: Sender Address Domain - vps.corporations.org 
X-Get-Message-Sender-Via: vps.corporations.org: authenticated_id: energy/only user confirmed/virtual account not confirmed 
X-Spam-Status: No, score=-96.2 
X-Spam-Score: -961 
X-Spam-Bar: --------------------------------------------------- 
X-Ham-Report: Spam detection software, running on the system "vortex.nocdirect.com", has 
identified this incoming email as possible spam. The original message 
has been attached to this so you can view it (if it isn't spam) or label 
similar future email. If you have any questions, see 
root\@localhost for details. 

Content preview: --1kf9a19f031518xd151gkd Content-Type: text/plain; charset=iso-8859-1 
    Content-Transfer-Encoding: 7bit Text Mesage version goes here More text message 
    --1kf9a19f031518xd151gkd Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: 
    7bit <html> <b> TEST</b> </html> [...] 

Content analysis details: (-96.2 points, 8.0 required) 

    pts rule name    description 
---- ---------------------- -------------------------------------------------- 
-100 USER_IN_WHITELIST  From: address is in the user's white-list 
-0.0 T_RP_MATCHES_RCVD  Envelope sender domain matches handover relay 
          domain 
    1.0 BAYES_40    BODY: Bayes spam probability is 20 to 40% 
          [score: 0.2211] 
    0.0 T_MIME_NO_TEXT   No (properly identified) text body parts 
    2.8 MIME_PHP_NO_TEXT  No text body parts, X-Mailer: PHP 
X-Spam-Flag: NO 





     --1kf9a19f031518xd151gkd 
     Content-Type: text/plain; charset=iso-8859-1 
     Content-Transfer-Encoding: 7bit 


Text Message version goes here 
     More text message 



     --1kf9a19f031518xd151gkd 
     Content-Type: text/html; charset=iso-8859-1 
     Content-Transfer-Encoding: 7bit 
     <html> 
     <b> TEST</b> 
     </html> 



     --1kf9a19f031518xd151gkd-- 

我也尝试添加标题。但是这并没有帮助。

回答

0

发送格式正确的电子邮件的标准是严格的。所以使用SwiftMailer或其他库最容易。

这就是说,我通过以下电子邮件解决了我的问题。

一些关键因素 - 您需要在消息部分中的每个Content-Transfer-Encoding:标头之后有一个空行。 mail()函数具有两部分的标题 - 1)常规标题和2)必须包含在消息文本中的标题,以正确地格式化文本/纯文本和文本/ html消息。我在这一行有几个空格,应该是空白的,并且会导致错误。我在消息中标题中有几个其他的空间字符,导致它失败。

From - Thu Jul 17 15:20:45 2014 
X-Account-Key: account2 
X-UIDL: UID17590-1394824486 
X-Mozilla-Status: 0001 
X-Mozilla-Status2: 00000000 
X-Mozilla-Keys:                     
Return-path: <[email protected]> 
Envelope-to: [email protected] 
Delivery-date: Thu, 17 Jul 2014 15:21:37 -0400 
Received: from vps.corporations.org ([69.73.133.204]:42354) 
    by vortex.nocdirect.com with esmtps (TLSv1:DHE-RSA-AES256-SHA:256) 
    (Exim 4.82) 
    (envelope-from <[email protected]>) 
    id 1X7rFE-001n6o-2z 
    for [email protected]; Thu, 17 Jul 2014 15:21:37 -0400 
Received: from energy by vps.corporations.org with local (Exim 4.82) 
    (envelope-from <[email protected]>) 
    id 1X7rFC-0000pm-As 
    for [email protected]; Thu, 17 Jul 2014 15:21:30 -0400 
To: [email protected] 
Subject: New Test Subject -- [email protected] 
X-PHP-Script: www.energyjustice.net/action/thanks.php for 68.57.17.14 
From: e e <[email protected]> 
Reply-To: [email protected] 
MIME-Version: 1.0 
X-Mailer: PHP/5.2.9 
Content-Type: multipart/alternative;boundary="1kf9a19f031518xd151gkd" 
Message-Id: <[email protected]> 
Date: Thu, 17 Jul 2014 15:21:30 -0400 
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report 
X-AntiAbuse: Primary Hostname - vps.corporations.org 
X-AntiAbuse: Original Domain - campusactivism.org 
X-AntiAbuse: Originator/Caller UID/GID - [513 513]/[47 12] 
X-AntiAbuse: Sender Address Domain - vps.corporations.org 
X-Get-Message-Sender-Via: vps.corporations.org: authenticated_id: energy/only user confirmed/virtual account not confirmed 
X-Spam-Status: No, score=-101.9 
X-Spam-Score: -1018 
X-Spam-Bar: --------------------------------------------------- 
X-Ham-Report: Spam detection software, running on the system "vortex.nocdirect.com", has 
identified this incoming email as possible spam. The original message 
has been attached to this so you can view it (if it isn't spam) or label 
similar future email. If you have any questions, see 
root\@localhost for details. 

Content preview: charset="us-ascii" Content-Transfer-Encoding: 7bit Text Message 
    charset="iso-8859-1" Content-Transfer-Encoding: 7bit Html Message [...] 

Content analysis details: (-101.9 points, 8.0 required) 

    pts rule name    description 
---- ---------------------- -------------------------------------------------- 
-100 USER_IN_WHITELIST  From: address is in the user's white-list 
-0.0 RP_MATCHES_RCVD  Envelope sender domain matches handover relay domain 
-1.9 BAYES_00    BODY: Bayes spam probability is 0 to 1% 
          [score: 0.0000] 
    0.0 HTML_MESSAGE   BODY: HTML included in message 
    0.0 MISSING_MIME_HB_SEP BODY: Missing blank line between MIME header and 
          body 
    0.0 TVD_SPACE_RATIO  TVD_SPACE_RATIO 
X-Spam-Flag: NO 


--1kf9a19f031518xd151gkd 
Content-Type: text/plain; 
charset="us-ascii" 
Content-Transfer-Encoding: 7bit 

Text Message 

--1kf9a19f031518xd151gkd 
Content-Type: text/html; 
charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<html> 
<b> Html Message</b> 
</html> 
--1kf9a19f031518xd151gkd-- 

我的PHP代码:

$sMessage="--1kf9a19f031518xd151gkd 
Content-Type: text/plain; 
charset=\"us-ascii\" 
Content-Transfer-Encoding: 7bit 

Text Message 

--1kf9a19f031518xd151gkd 
Content-Type: text/html; 
charset=\"iso-8859-1\" 
Content-Transfer-Encoding: 7bit 

<html> 
<b> Html Message</b> 
</html> 
--1kf9a19f031518xd151gkd--"; 


//The email headers include the sender's name and email 
$sHeaders = "From: ".$aRow['sFname']." ".$aRow['sLname']." <".$aRow['sEmail'].">" . " \r\n" . 
"Reply-To: ".$aRow['sEmail']." \r\n" . 
"MIME-Version: 1.0 \r\n" . 
'X-Mailer: PHP/' . phpversion() ." \r\n" . 
"Content-Type: multipart/alternative;". 
"boundary=\"1kf9a19f031518xd151gkd\""."\r\n"; 

mail($value, $aRow_alert['sSubject'], $sMessage, $sHeaders); 
0

如果你想用PHP发送邮件,你可以做的最好的事情就是找到一个好的邮件处理库。当一大群人已经为你简化它时,为什么重新开发邮件处理?

我一直在从头做起。我现在使用SwiftMailer。还有其他的 - 我已经尝试了很多 - 但这对我来说最合适。

+0

我不需要大多认为SwiftMailer具有的功能,但我可能会尝试一下。使用mail()发送电子邮件并不难。我已经完成了文本和HTML消息 - 只是没有在同一时间完成。 –

+0

对。使用mail()发送简单的电子邮件并不困难。开始做更多的事情(密件抄送,附件等),并且您开始在邮件代码上花费大量额外时间,这些邮件代码更适合您自己的应用程序代码。我不再担心第三方代码具有额外的功能,我现在不需要/现在/ - 迟早会出现(它总是这样),我已经熟悉了这个库。 – DinoAmino

相关问题