将多行发送到一封电子邮件

时间:2017-01-31 09:20:53

标签: php mysql pdo

拜托,我有一个向客户发送电子邮件的脚本。该脚本的问题在于它发送了多封邮件,但我想要的只是它发送一封包含多行详细信息的邮件。即所有账单明细应该在表格中。

下面是我的脚本

由于

$bill_status = "OVERDUE";
$st = $pdo->prepare("SELECT *, DATEDIFF(NOW(), due_date) AS days_diff 
FROM bills LEFT JOIN login_details ON bills.user_id = login_details.user_id
WHERE bills.bill_status = '$bill_status'");
$st->execute();
$row_sent = $st->rowCount();

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;  // Enable verbose     debug output

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;    // Enable SMTP authentication
$mail->Username = 'mails@mail.com';                 // SMTP username
$mail->Password = 'pasword';                 // SMTP password
$mail->SMTPSecure = 'ssl'; 
$mail->Port = 465;                   // TCP port to connect to
$mail->From = 'no_reply@noreply.com';
$mail->FromName = 'Coy Name';

$mail->WordWrap = 587;        // Set word wrap to 50 characters
$mail->AddEmbeddedImage("../img/logo.png", "logo");
$mail->isHTML(true);      // Set email format to HTML
$mail->Subject = 'Notice';
while($rows = $st->fetch(PDO::FETCH_ASSOC)){
$username = $rows['username'];

$message= "
<div class='messages'>
<h3><img src='cid:my_logo'> </h3>
<br>
<div style='font-size:15px;'>Email Notification on Bill Due for Payment      </h4>
<p>Dear: Customer</p>
<p>
The following bills are due for payment:
</p>
<table width='80%' border='0' cellspacing='0' cellpadding='0'>
<tr style='font-weight:bold;'>
<td>Trans Ref</td>
            <td>Due Date</td>
            <td>Days Overdue</td>
            <td>Service Provider</td>
            <td>Service Type</td>
            <td>Amount Paid</td>
            <td>Bill Status</td>
            <td>Recurring</td>
  </tr>

 <h3>Yours<br>
Coy Name</h3>
";    

$message .= "        <tr>";
$message .= "            <td>".$rows['trans_ref']."</td>";
$message .= "            <td>".$rows['due_date']."</td>";
$message .= "            <td>".$rows['days_diff']."</td>";
$message .= "            <td>".$rows['service_provider']."</td>";
$message .= "            <td>".$rows['service_type']."</td>";
$message .= "            <td>".$rows['amount_paid']."</td>";
$message .= "            <td>".$rows['bill_status']."</td>";
$message .= "            <td>".$rows['recurring']."</td>";
$message .= "        </tr>";

$mail->addAddress($username);     // Add a recipient
$mail->Body    = $message;
$mail->send();
$mail->ClearAddresses();

}

1 个答案:

答案 0 :(得分:0)

while循环应该只在

附近
$message .= "        <tr>";
....
$message .= "        </tr>";

语句。否则所有其他语句也会被执行多次。