0

嗨,我已经编写了一个脚本来从 MySQL 中获取数据并将其邮寄。我正在使用 HTML 内容类型我的问题是输出 HTML 内容格式

这是我的代码

<?php
$con=mysqli_connect("host","user","pwd","db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . Mysqli_connect_error();
  }

$result3 = mysqli_query($con,"SELECT  cardnumber, email, firstname, surname, GROUP_CONCAT(borrowernumber) as borrowernumber, GROUP_CONCAT(issuedate) as issuedate, GROUP_CONCAT(date_due) as date_due,  GROUP_CONCAT(barcode) as barcode, GROUP_CONCAT(title SEPARATOR '//') as Title, GROUP_CONCAT(author SEPARATOR '/') as Author FROM issuestestmail GROUP BY email");
while($row = mysqli_fetch_array($result3))
{
$to = $row['email'];
$emailBody="<html><body><font face='arial' size='2'>";
// explode the Pin/balances on the comma's
$borrowernumber = explode(',',$row['borrowernumber']);
$barcode = explode(',',$row['barcode']);
$issuedate = explode(',',$row['issuedate']);
$date_due = explode(',',$row['date_due']);
$Title = explode('//',$row['Title']);
$Author = explode('/',$row['Author']);
$subject = "List of Issued Books ( Total : ".count($barcode).")";
// Create a line for each item/balance
$emailBody .= "<b>Dear,</b>  ".$row['firstname']." ".$row['surname']."  ("  ."<b>ID:</b> <b>".$row['cardnumber']."</b>)<br/><br/>"."\r\n"."Kindly find the list of library items which are currently issued on your account"."<br/><br/>";

    $emailBody .= "<b>Total no of issued books:</b> ".count($barcode)."<br/><br/>";

    foreach($borrowernumber as $key => $borrowernumber){

    $emailBody .= "<table border='0' width='100%' id='table1' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Barcode</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$barcode[$key]."</td>";
    $emailBody .= "</tr></table>";

    $emailBody .= "<table border='0' width='100%' id='table2' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Title</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$Title[$key]."</td>";
    $emailBody .= "</tr></table>";

    $emailBody .= "<table border='0' width='100%' id='table3' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Author</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$Author[$key]."</td>";
    $emailBody .= "</tr></table>";

    $emailBody .= "<table border='0' width='100%' id='table4' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Issue date</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$issuedate[$key]."</td>";
    $emailBody .= "</tr></table>";

    $emailBody .= "<table border='0' width='100%' id='table5' style='border-width: 0px; font-size:14px'><tr>";
        $emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Due Date</b></td>";
    $emailBody .= "<td style='border-style: none; border-width: medium'>: ".$date_due[$key]."</td>";
    $emailBody .= "</tr></table><br/><hr><br/>";}

// add a Total Balance line

$emailBody .= "<br/><br/><b>Library</b><br/>xyz<br/>";
$emailBody .= "</font></body></html>";
$headers = "From: Library@lotus.edu.in\r\n";
$headers .= "Reply-To: Library@lotus.edu.in\r\n";
$headers .= "Return-Path: Library@lotus.edu.in\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

      if(mail($to, $subject, $emailBody, $headers)) {
          echo $emailBody;
          echo 'Email sent successfully!<br/><br/>';
      } else {
          echo $emailBody;
          die('Failure: Email was not sent!');
      }

}

mysqli_close($con);
?>

现在这封邮件的输出在 gmail 上是这样的

一切都很好,但我在第 3 条记录“”< table border='0' width='100%' ID='table3' style='border-width: 0px; 字体大小:14px'>""

在此处输入图像描述

4

1 回答 1

1

1) 使用此行使用 base64 对整个消息进行编码:

$emailBody= chunk_split(base64_encode($emailBody));

然后,将此附加到您的标题:

$headers .= "Content-Transfer-Encoding: base64\r\n\r\n";

这将告诉邮件客户端您的邮件是 base64 编码的。

其实这里,

""< table border='0' width='100%' id='table3' style='border-width: 0px; font-size:14px'>""

不知何故,在两者之间添加了空间<table这导致了这个错误。

2)另外,我在您的标记中发现了另一个小问题,即width='100'在以下行中:

$emailBody .= "<td width='100' style='border-style: none; border-width: medium'><b>Due Date</b></td>";

将其更改为 width='100%' 或 width='100px' 您希望输出的任何内容。

3)根据您评论的要求,(即跳过从查询中找不到电子邮件地址的电子邮件)您只需要用if支票包装您的那部分

例如:

while($row = mysqli_fetch_array($result3))
{
if($row['email'])
{
$to = $row['email'];
$emailBody="<html><body><font face='arial' size='2'>";
.
.
.
.
      if(mail($to, $subject, $emailBody, $headers)) {
          echo $emailBody;
          echo 'Email sent successfully!<br/><br/>';
      } else {
          echo $emailBody;
          die('Failure: Email was not sent!');
      }
}
}
于 2013-06-15T09:50:47.257 回答