I have a mysql db with stored a table with some columns about person informations as
- object1 (p.k)
- username
- service
- sent
With a php code for phpmailer I can connect to db, extract informations that I need from a sql query and sent email for every person through a foreach function, after every email is sent correctly, data for column "sent" is update from 0 to 1... all works fine, but there's possibility there're some people with same "emails" for multiple own "object1" and in this case, for every rows will be sent an email to them...
For example if one person with email aa@aa.it has 30 "object1", he'll receive 30 different emails.
How can I combine work with data from query and do all of these object1 associated to one email are sent in only one email with list all of theme as content?
I think that I need to complete script with a if/else istructions, but I don't know how manage it.
Below part of script to do it:
<?
$mail = new PHPMailer;
$result = mysqli_query($mysql, 'SELECT * FROM db WHERE service LIKE \'%something%\' AND sent = 0 ORDER BY object1 LIMIT 0 , 30');
foreach ($result as $row) {
$mail->addAddress($row['email'], $row['object1']);
/* i use it to use an external .html file to send email and replace vairables in it */
$message = file_get_contents('template/temaplate.html');
$message = str_replace('%email%', $row['email'], $message);
$message = str_replace('%object1%', $row['object1'], $message);
//Set the message
$mail->MsgHTML($message);
$mail->AltBody = strip_tags($message);
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo .;
break; //Abandon sending
} else {
echo "Message sent to :" . $row['object1'] . ' (' . str_replace("@", "@", $row['email']) .;
//Mark it as sent in the DB
mysqli_query(
$mysql,
"UPDATE db SET sent = true WHERE object1 = '" .
mysqli_real_escape_string($mysql, $row['object1']) . "'"
);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
$mail->clearAttachments();
}
?>
I hope to have explained correctly my needs and ask you some helps and suggestions for it.