title: Using array variable in $mailer->AddAddress("user@domain.com"); PHPMailer script
body: <p>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. </p>
  
  <p>The script works but only sends email to the first recipient found in the database. Any clues on where I am screwing up? THX!</p>
  
  <pre><code>  $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-&gt;IsSMTP();
      $mailer-&gt;Host = 'ssl://smtp.gmail.com:465';
      $mailer-&gt;SMTPAuth = TRUE;
      $mailer-&gt;Username = 'myemail@gmail.com';  // Sender's gmail address
      $mailer-&gt;Password = 'the_password';  // Sender's gmail password
      $mailer-&gt;From = "$from";  // Sender's email address
      $mailer-&gt;FromName = "$from_name"; // senders name 
      $mailer-&gt;Body = "$msg";
      $mailer-&gt;Subject = "$subject";
      $mailer-&gt;AddAddress("$to");  // Recipient
        if(!$mailer-&gt;Send())
      {
         echo 'Email sent to:' . $to . '&lt;br/ &gt;';
         echo "Mailer Error: " . $mailer-&gt;ErrorInfo;
      }
      else
      {
         echo 'Email sent to:' . $to . '&lt;br/ &gt;';
      }
  
  
    mysqli_close($dbc);
  </code></pre>