i use the following code to send mail with PHPMailer.It sends the emails to the recipients from db ,but it sends many emails to them.I want to send them only 1 with the related records in the HTML Table for each user.eg. if there are 5 records returned for a user then it sends 5 emails to the user with 1 record inside.
Also,i don't want to use the try and catch commands the default PHPMailer script uses,but while ...
How can i change it to do what i want? Thank you.
<?php
$stmt = sqlsrv_query( $conn, $sql );
$rows = array();
while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){
$rows[] = $row;
//foreach ($result as $row) {
try {
$mail->addAddress($row['email']);
}
catch (Exception $e) {
echo 'Invalid address skipped: ' . htmlspecialchars($row['email']) . '<br>';
continue;
}
try {
$body = 'Hi: '. $row['first'] .' '. $row['last'] .'<br>';
$body .= '<table border="1"><tr><th>Username</th><th>Day</th><th>Date</th></tr><th>Action</th>';
$body .= '<tr><td>';
$body .= $row['username'] . '</td><td>';
$body .= $row['day'] . '</td><td>';
$body .= $row['date'] . '</td><td>';
$body .= $row['action'] . '</td></tr>';
$body .= '</table>';
$mail->msgHTML($body);
$mail->send();
echo 'Message sent to :' . htmlspecialchars($row['last']) . ' '. htmlspecialchars($row['first']) . '(' . htmlspecialchars($row['email']) . ')<br>';
} catch (Exception $e) {
echo 'Mailer Error (' . htmlspecialchars($row['email']) . ') ' . $mail->ErrorInfo . '<br>';
$mail->smtp->reset();
}
$mail->clearAddresses();
$mail->clearAttachments();
}
?>

$row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOCreturns all the records for all users.You mean toGROUP BYin the SQL Statement?I think it needs ane more loop inside thewhilecommand.