开发者

Using array variable in $mailer->AddAddress("user@domain.com"); PHPMailer script

开发者 https://www.devze.com 2023-02-15 11:49 出处:网络
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 \'

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);


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);


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 :)


In my way, i did this:

$to = $row['email'];

$mailer->AddAddress("{$to}");  // Recipient
0

精彩评论

暂无评论...
验证码 换一张
取 消