Otto
Otto

Reputation: 119

PHPMailer: Send emails to multiple recipients error

As in the title, I can't send an email to multiple recipients because I retrieve the following error:

Message could not be sent. Mailer Error: You must provide at least one recipient email address.

I'm using this code:

$stmt=$db->prepare("select email from app_users where enabled=1");
if (!$stmt) {
  log_msg($db->error);
  die();
}
if (!test_execute($stmt->execute())) die("ERROR");
$emailto=fetchArray2($stmt);
$stmt->close();
foreach ($emailto as $contacts) {
  $mail->AddAddress($contacts);
}

What's wrong?

print_r($emailto);

Array
(
[0] => Array
    (
        [email] => [email protected]
    )

[1] => Array
    (
        [email] => [email protected]
    )

[2] => Array
    (
        [email] => [email protected]
    )

)

Upvotes: 0

Views: 616

Answers (1)

Synchro
Synchro

Reputation: 37700

@Natrium nearly had it. You're not accessing your array elements correctly. Do it like this:

foreach ($emailto as $contacts) {
  $mail->addAddress($contacts['email']);
}

It's also a good idea to check return values, perhaps:

foreach ($emailto as $contacts) {
  if (!$mail->addAddress($contacts['email'])) {
    echo 'Address rejected: '.$contacts['email'];
  }
}

Upvotes: 1

Related Questions