dragon0023 2016-06-14 12:15
浏览 40

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

I got an issue with PHPmailer (5.2.14) and with it corrupting complex html email bodys.

I will attach a snippet of the email geneartion process below but I will explain the issue first.

When I send out emails the body of the text gets corrupt and random whitespaces are added, text goes bold or html tags are completely ignored like this:

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


I break up my email body into a $message string variable.

However when I print out $message I cannot see any issues with $message

When I var_dump() $message the email appears in the browser perfectly with no issues like this:

enter image description here enter image description here


Anyone have any idea why PHPmailer is corrupting $message when sending the email? I have tried multiple things but the only thing I have been able to conclude is that the issues doesn't lie with $message. The issue is sporadic. It appears to go wrong in different places each time the email is generated

And If I run the php file within linux. and echo $message. I can see no issues with the HTML. I.e. no whitespace weird html tags etc. If I var_dump() $message it displays with no issues within the browser. If I view the email within outlook and on my iPhone the same email is corrupt the same way on both devices.

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条回答 默认 最新

  • dongni8969 2016-06-14 14:07
    关注

    Changing the encoding to base64 seemed to resolve the issue.

    $mail->Encoding = 'base64';
    

    For future reference:

    When you send, take a copy of the sent message by calling $mail->getSentMIMEMessage() after sending and compare it with what you receive - if there is a difference then the problem is occurring after the message has left PHPMailer's control. Outlook has been known to corrupt quoted-printable encoding, so you could see if setting $mail->Encoding = 'base64'; makes a difference

    Credit goes to Synchro

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集