user3304303 user3304303 - 5 months ago 89
SQL Question

How to send PHP mailer to multiple email addresses received via MySQL query

I run a MySQL query to get firstname, lastname and email from a table where 'notify' is set to YES, like this below. And in the while loop, I create all the info which I then put into an array right after:

$sql = "SELECT firstname, lastname, email, notify FROM guesses
WHERE poolid = '$poolid'
AND notify = 'yes'";
$getnotify = mysqli_query($connection, $sql);
if (!$getnotify) {
die("Database query failed: " . mysqli_error());
} else {
while ($row = mysqli_fetch_array($getnotify)) {
$notifyemailscontent.="'".$row['email'] . "' => '" . $row['firstname'] . " " . $row['lastname']. "',";


Then in PHP mailer code, I always send to my address, like this:

$mail->addAddress(, 'Me');

But then I also want to add the emails I get in the mysql query (whether there are 1 or 100) as CC or BCC (either is fine). I tried doing it like this below, based on something I found online, but it's not behaving as I would like.

foreach($notifyemails as $email => $name)
$mail->AddBCC($email, $name);

NOTE: It IS sending the email to ME, but it's not sending the email to the BCC people. When I print the $notifyemails array, I get the following (only one BCC email in this case):

Array ( [0] => '' => 'Bob Jones', )

Again, I get the email, but Bob is NOT BCC'd on it. So I think something has to be wrong in the for loop above or possibly in the mysql query loop at the top??? Any insight/direction would be appreciated.


Given the other answers, am submitting the following.

As I stated in comments, have pulled from their example the following and slightly modified to also show the person's name should you want to use that.

You can add to it also, using different columns while assigning a variable to them.

Borrowed from

N.B.: You will need to modify the query/column names slightly and possibly some of the code since you did not post your full code.

The following is a working example of what I used, and hope it serves you well.


include ('/path/to/database_connection.php');

error_reporting(E_STRICT | E_ALL);

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->Host = 'xxx';
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead

$mail->SMTPSecure = 'tls'; // if required
$mail->Port = 587; // or use the preferred port of your choice
$mail->Username = 'xxx';
$mail->Password = 'xxx';

$mail->addAddress('', 'John');
$mail->setFrom('', 'List manager');
$mail->addReplyTo('', 'List manager');
$mail->Subject = "PHPMailer Simple database mailing list test";
//Same body for all messages, so set this before the sending loop
//If you generate a different body for each recipient (e.g. you're using a templating system),
//set it inside the loop

//msgHTML also sets AltBody, but if you want a custom one, set it afterwards
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';

$result = mysqli_query($connection, "SELECT user, email FROM table WHERE col = 'x'");

    foreach ($result as $row) { //This iterator syntax only works in PHP 5.4+

        $user = $row['user'];

        $body = "Hello $user, <br><br>This is the HTML message body <b>in bold!</b>";


        $mail->addBCC($row['email'], $row['user']);

        if (!empty($row['photo'])) {
            $mail->addStringAttachment($row['photo'], 'YourPhoto.jpg'); //Assumes the image data is stored in the DB

        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent to:" . $row['user'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
            //Mark it as sent in the DB

    /* UPDATE the table if needed
                "UPDATE mailinglist SET sent = true WHERE email = '" .
                mysqli_real_escape_string($connection, $row['email']) . "'"

        // Clear all addresses and attachments for next loop