PHPmailer破坏HTML正文消息。 (错误?)

时间:2016-06-14 12:15:04

标签: php html css email phpmailer

我遇到了PHPmailer(5.2.14)的问题,并且它破坏了复杂的html电子邮件机身。

我将在下面附上电子邮件发送流程的片段,但我会先解释这个问题。

当我发送电子邮件时,文本正文被破坏并添加随机空格,文本变为粗体或html标记完全被忽略,如下所示:

enter image description here enter image description here enter image description here

我将我的电子邮件正文拆分为$message字符串变量。

但是,当我打印$message时,我看不到$message

的任何问题

当我var_dump()$message时,电子邮件会完全显示在浏览器中,没有这样的问题:

enter image description here enter image description here

任何人都知道为什么PHPmailer在发送电子邮件时会损坏$ message?我尝试了多种方法,但我唯一能够得出的结论是问题不在于$message。这个问题是零星的。每次生成电子邮件时,它似乎在不同的地方出错

如果我在linux下运行php文件。和echo $消息。我看不出HTML的问题。即没有空格奇怪的html标签等。如果我在var_dump()$ message中显示它在浏览器中没有问题。如果我在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('contact@BLAH.com', 'BLAH');
$mail->addAddress('BLAH@BLAH.co.uk');
$mail->addReplyTo('BLAH@BLAH.com', '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';
}                   

}

1 个答案:

答案 0 :(得分:0)

将编码更改为base64似乎解决了这个问题。

$mail->Encoding = 'base64';

供将来参考:

  

发送时,在发送后通过调用$mail->getSentMIMEMessage()获取已发送消息的副本,并将其与您收到的内容进行比较 - 如果存在差异,则在消息离开PHPMailer的控制之后问题就出现了。众所周知,Outlook会破坏带引号的可打印编码,因此您可以看到设置$mail->Encoding = 'base64';是否有所作为

信用转到Synchro