2016-06-14 85 views
0

我得到了一個PHPmailer(5.2.14)的問題,並與它腐蝕複雜的HTML電子郵件身體。PHPmailer破壞HTML正文消息。 (bug?)

我會在下面附上電子郵件合作過程的一個片段,但我會先解釋這個問題。

當我發送電子郵件的正文中得到腐敗,並添加隨機的空格,文雲粗體或HTML標籤是完全忽視這樣的:

enter image description hereenter image description hereenter image description here


我打破將我的電子郵件正文變成$message字符串變量。

然而,當我打印出來$message我看不出任何問題$message

當我的var_dump()$message的電子郵件出現在瀏覽器完美,像這樣沒有任何問題:

enter image description hereenter image description here


任何人都知道爲什麼PHPmailer發送郵件時會破壞$消息?我已經嘗試了很多東西,但我唯一能夠得出結論的是這些問題不在於$message。這個問題是零星的。它在每次生成電子郵件時在不同的地方出現錯誤

而且如果我在Linux內運行php文件。並echo $消息。我看不到HTML的問題。即沒有空白的怪異的html標籤等。如果我var_dump()$消息顯示在瀏覽器中沒有問題。如果我在outlook和我的iPhone中查看電子郵件,則同一封電子郵件在兩臺設備上都會以同樣的方式損壞。

if ($monthlyEmail == TRUE){ //If the monthlyEmail trigger is triggered lets send out the monthly report 

$Query = "SELECT * FROM CAPS_Accounts WHERE TypeOfBusiness = 'CHARGE SERVICE'"; 
$result = mysqli_query($db, $Query); 
$rows = mysqli_num_rows($result); 

$start_date_month = new DateTime("first day of last month"); 
$end_date_month = new DateTime("last day of last month"); 

$start = $start_date_month->format('Y-m-d'); 
$end = $end_date_month->format('Y-m-d'); 

$totalTransmissions = 0; 

$query_transmission = "SELECT * FROM CAPS_CollectedCases WHERE FirstUpdate >= '$start' AND FirstUpdate <= '$end'"; 
$result_transmission = mysqli_query($db, $query_transmission); 
$rows_transmission = mysqli_num_rows($result_transmission); 
if ($rows_transmission != 0) { 
    for ($i = 0; $i < $rows_transmission; $i ++) { 
     $row_transmission = clean_fetch_assoc($result_transmission); 
     $NumberUpdates = $row_transmission['NumberUpdates']; 
     $totalTransmissions = $totalTransmissions + $NumberUpdates; // Add to the transmission count. 
    } 
} 

$message = ' 
<html> 
    <head> 
     <title>BLAH BLAH</title> 
    </head> 
    <body> 
     <div style="display:none;">BLAH Notification Email</div> 
     <table style="margin: 0 auto; width:100%; background-color:#eeeeee; font-family: Tahoma,Verdana,Segoe,sans-serif; text-align:center; font-size:12px;"> 
      <tr style="background-color:#FFFFFF;"> 
       <td><img src="BLAH.png" style="width:300px;"> 
      </tr> 
      <tr style="text-align:left;"> 
       <td style="padding:10px;"> 
        Dear ' . 'BLAH' . ',<br /><br /> 
        This is the mnonthly stats carried out at: ' . date("d/m/Y @ H:i:s") . ' for month ' . date_format($start_date_month,'M Y') . ' <br /> 
        Total claims created this month: ' . $rows_transmission . '<br /> 
        Total unique transmissions delivered this month: ' . $totalTransmissions . '<br /><br /> 
'; 

do { //For some stupid reason a for loop didn't want to work. I will look at this at some point but sod it. 
    $BusinessID = $row['BusinessID']; 
    if ($BusinessID != '') { 

     $query_calc = "SELECT * FROM CAPS_DTCInvoice WHERE BusinessID = '$BusinessID' ORDER BY InvNumber DESC LIMIT 1,1"; //Select the 2nd to latest invoice again in case we just inserted a new one. 
     $result_calc = mysqli_query($db, $query_calc); 
     $rows_calc = mysqli_num_rows($result_calc); 
     $row_calc = clean_fetch_assoc($result_calc); 
     $InvNumber = $row_calc['InvNumber']; 
     $Rate = $row_calc['Rate']; 
     $VATRate = $row_calc['VATRate']; 

     $UniqueTransmissions = 0; //Initialise this badboy. This will contain the number of transmissions being added and are already within this months invoice. 

     //Lets tally our existing unique transmissions before looking for new transmissions to add to the invoice. 
     $query_transmission = "SELECT * FROM CAPS_CollectedCases WHERE BusinessID = '$BusinessID' AND InvNumber = '$InvNumber' "; 
     $result_transmission = mysqli_query($db, $query_transmission); 
     $rows_transmission = mysqli_num_rows($result_transmission); 
     if ($rows_transmission != 0) { 
      for ($i = 0; $i < $rows_transmission; $i ++) { 
       $row_transmission = clean_fetch_assoc($result_transmission); 
       $NumberUpdates = $row_transmission['NumberUpdates']; 
       $UniqueTransmissions = $UniqueTransmissions + $NumberUpdates; // Add to the transmission count. 
      } 
     } 

     //Lets Start populating the invoice row with the data now we have refreshed the data counts. 
     //Number of Claims 
     $query_transmission = "SELECT CCaseIndex FROM CAPS_CollectedCases WHERE BusinessID = '$BusinessID' AND InvNumber = '$InvNumber'"; 
     $result_transmission = mysqli_query($db, $query_transmission); 
     $DelCount = mysqli_num_rows($result_transmission); 

     //Earliest Transmission 
     $query_transmission = "SELECT FirstUpdate FROM CAPS_CollectedCases WHERE BusinessID = '$BusinessID' AND InvNumber = '$InvNumber' ORDER BY FirstUpdate LIMIT 1 "; 
     $result_transmission = mysqli_query($db, $query_transmission); 
     $row_transmission = clean_fetch_assoc($result_transmission); 
     $FirstDelDate = $row_transmission['FirstUpdate']; 

     //Latest Transmission 
     $query_transmission = "SELECT LastUpdate FROM CAPS_CollectedCases WHERE BusinessID = '$BusinessID' AND InvNumber = '$InvNumber' ORDER BY LastUpdate DESC LIMIT 1 "; 
     $result_transmission = mysqli_query($db, $query_transmission); 
     $row_transmission = clean_fetch_assoc($result_transmission); 
     $LastDelDate = $row_transmission['LastUpdate']; 

     //Current Cost 
     $NettTotal = $DelCount * $Rate; 
     $VATValue = ($NettTotal/100) * $VATRate; 
     $VATAmount = $NettTotal + $VATValue; 

     $query_view_invoice = "SELECT * FROM CAPS_DTCInvoice WHERE BusinessID = '$BusinessID' ORDER BY InvNumber DESC LIMIT 1,1"; //Select the 2nd to latest invoice again in case we just inserted a new one. 
     $view_invoice = mysqli_query($db, $query_view_invoice); 
     $row_view_invoice = clean_fetch_assoc($view_invoice); 

     $message .= "---------------<br />";  
     $message .= "Service ID: " . $row['BusinessID'] . " - " . $row['BusinessName'] . "<br />"; 
     $date = date_create($row_view_invoice['InvoiceDate']); 
     $message .= " Current invoice and statistics from date commencing: " . date_format($date, "d/m/Y") . "<br />"; 
     $message .= "---------------<br />"; 
     $message .= "Invoice ID: " . $row_view_invoice['InvNumber'] . "<br />"; 
     if ($row_view_invoice['FirstDelDate'] != '0000-00-00 00:00:00'){ 
      $date = date_create($row_view_invoice['FirstDelDate']); 
      $message .= "First Delivery Date: " . date_format($date, "d/m/Y @ H:i:s") . "<br />"; 
     }else{ 
      $message .= "First Delivery Date: N/A <br />"; 
     } 
     if ($row_view_invoice['LastDelDate'] != '0000-00-00 00:00:00'){ 
      $date = date_create($row_view_invoice['LastDelDate']); 
      $message .= "Last Delivery Date: " . date_format($date, "d/m/Y @ H:i:s") . "<br />"; 
     }else{ 
      $message .= "Last Delivery Date: N/A <br />"; 
     } 
     $message .= "Transmitted Claim Count: " . number_format($row_view_invoice['DelCount']) . "<br />"; 
     $message .= "Unique Transmissions: " . number_format($UniqueTransmissions) . "<br />"; 
     $message .= "Rate: &#163;" . $row_view_invoice['Rate'] . "<br />"; 
     $message .= "VAT Rate: " . $row_view_invoice['VATRate'] . "%<br />"; 
     $message .= "Month Net Total: &#163;" . $row_view_invoice['NettTotal'] . "<br />"; 
     $message .= "VAT Total: &#163;" . $VATValue . "<br />"; 
     $message .= "Month Gross Total: &#163;" . $row_view_invoice['VATAmount'] . "<br />"; 
     $message .= "---------------<br /><br />";  
    } 
} while ($row = clean_fetch_assoc($result)); 

$message .=   ' If you have any questions you can contact BLAH BLAH <br /><br /> 
           Many Thanks,<br /> 
           BLAH BLAH 
          </td> 
         </tr> 
         <tr style="background-color:#1175BA; color:#FFFFFF; font-size:12px;"> 
          <td>BLAH BLAH &copy; 2016</td> 
         </tr> 
         </table> 
       </body> 
      </html> 
     '; 

var_dump($message); 

$mail = new PHPMailer; 

$mail->setFrom('[email protected]', 'BLAH'); 
$mail->addAddress('[email protected]'); 
$mail->addReplyTo('[email protected]', 'BLAH'); 
$mail->isHTML(true);        

$mail->Subject = ' Monthly BLAH Stats'; 
$mail->Body = $message; 

if(!$mail->send()) { 
    echo 'Monthly Email could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Monthly Email has been sent'; 
}     

} 
+0

您使用哪個PHPMailer的版本?你如何確定問題不在'$ message'中?電子郵件客戶端非常多樣,在解釋HTML/CSS代碼方面相當糟糕。僅僅因爲它在瀏覽器中工作**並不意味着相同的代碼將在電子郵件中工作 – Martin

+0

如果我在Linux中運行php文件。並echo $消息。我看不到HTML的問題。即沒有空白的怪異的html標籤等。如果我var_dump()$消息顯示在瀏覽器中沒有問題。 如果我在outlook和我的iPhone上查看電子郵件,那麼同一封電子郵件在兩臺設備上都會以同樣的方式損壞。 我正在使用'phpMailer 5.2.14' – Ryan

+0

你可以添加一個有用的評論給你的問題。您在哪些電子郵件客戶端上查看了輸出HTML? – Martin

回答

0

將編碼更改爲base64似乎可以解決問題。

$mail->Encoding = 'base64'; 

以供將來參考:

當您發送,以發送郵件的副本發送後調用$mail->getSentMIMEMessage()並將其與您收到什麼比較 - 如果是有區別的,那麼問題是在消息離開PHPMailer的控制之後發生。展望已經知道腐敗的引用可打印的編碼,所以你可以看到,如果設置$mail->Encoding = 'base64';有差別

幸得Synchro