0

I am trying to write PHPMailer to send email. I am using a while loop with mysqli_fetch_array to extract email records from MySQL and have assigned the 'email' database field to a variable called '$to' and feeding to the PHPMailer $mailer->AddAddress("user@domain.com") call.

The script works but only sends email to the first recipient found in the database. Any clues on where I am screwing up? THX!

  $from = 'myemail@gmail.com';
  $from_name = 'My Name';
  $subject = $_POST['subject'];
  $text = $_POST['elvismail'];

$dbc = mysqli_connect('localhost', 'username', 'the_password', 'database_name')
    or die('Error connecting to mysql');

$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
  or die('Error querying database.');

    while ($row = mysqli_fetch_array($result)){
      $to = $row['email'];
      $first_name = $row['first_name'];
      $last_name = $row['last_name'];
      $msg = "Dear $first_name $last_name,\n$text";
    } 


    require("PHPMailer_v5.1 2/class.phpmailer.php");
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->Host = 'ssl://smtp.gmail.com:465';
    $mailer->SMTPAuth = TRUE;
    $mailer->Username = 'myemail@gmail.com';  // Sender's gmail address
    $mailer->Password = 'the_password';  // Sender's gmail password
    $mailer->From = "$from";  // Sender's email address
    $mailer->FromName = "$from_name"; // senders name 
    $mailer->Body = "$msg";
    $mailer->Subject = "$subject";
    $mailer->AddAddress("$to");  // Recipient
      if(!$mailer->Send())
    {
       echo 'Email sent to:' . $to . '<br/ >';
       echo "Mailer Error: " . $mailer->ErrorInfo;
    }
    else
    {
       echo 'Email sent to:' . $to . '<br/ >';
    }


  mysqli_close($dbc);
webbiedave
  • 48,414
  • 8
  • 88
  • 101
ven
  • 395
  • 3
  • 9
  • 17
  • This has been answered here before. Take a look at http://stackoverflow.com/questions/1770765/phpmailer-addaddress – New Guy Mar 08 '11 at 23:56

3 Answers3

1

You're closing the while loop too soon.

Change it to:

require("PHPMailer_v5.1 2/class.phpmailer.php");
while ($row = mysqli_fetch_array($result)){
    $to = $row['email'];
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $msg = "Dear $first_name $last_name,\n$text";

    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->Host = 'ssl://smtp.gmail.com:465';
    $mailer->SMTPAuth = TRUE;
    $mailer->Username = 'myemail@gmail.com';  // Sender's gmail address
    $mailer->Password = 'the_password';  // Sender's gmail password
    $mailer->From = "$from";  // Sender's email address
    $mailer->FromName = "$from_name"; // senders name 
    $mailer->Body = "$msg";
    $mailer->Subject = "$subject";
    $mailer->AddAddress("$to");  // Recipient
    if(!$mailer->Send())
    {
        echo 'Email sent to:' . $to . '<br/ >';
        echo "Mailer Error: " . $mailer->ErrorInfo;
    }
    else
    {
        echo 'Email sent to:' . $to . '<br/ >';
    }


// Then close your while loop here
} 

mysqli_close($dbc);
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • You're welcome. Don't forget to upvote and accept answers. It's how to show appreciation on stack overflow! – webbiedave Mar 23 '11 at 00:22
0

In my way, i did this:

$to = $row['email'];

$mailer->AddAddress("{$to}");  // Recipient
Mittal Patel
  • 2,732
  • 14
  • 23
youpi
  • 1
0

At the end of your loop, $to (and the other variables) will be set to the last iteration of the loop.

Adjust your code so you initialise PHPMailer above the loop, and then call $mailer->AddAddress($to) within your loop.

Don't forget to call $mailer->Send() after your loop has finished :)

alex
  • 479,566
  • 201
  • 878
  • 984