Search code examples
phpemailphpmailer

Using array variable in $mailer->AddAddress("[email protected]"); PHPMailer script


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("[email protected]") 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 = '[email protected]';
  $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 = '[email protected]';  // 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);

Solution

  • 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 = '[email protected]';  // 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);