Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing array variable in $mailer->AddAddress("user@domain.com"); PHPMailer script
    text
    copied!<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>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload