title: dynamic content in Email using PHPmailer
body: <p>I have this mailing list from php mailer examples: <a href="https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps" rel="nofollow noreferrer">https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps</a></p>
  
  <p>so I put the foreach loop within a while loop so it iterates through the entire table, I'm using a token array to replace the name and any dynamic content, the content is working, but it keeps putting the wrong name in the email.</p>
  
  <p>The name and email are inserted into <code>vac_mailing_system</code> table correctly. but when it's selected it seems to grab a random name and assign it to a random email address. So this makes me think there is an issue with the select statement. </p>
  
  <pre><code> $finish_table = $dbh-&gt;prepare("SELECT * FROM vac_mailing_system WHERE sent_status = false AND retry_attempts &lt; 5");  
  
  if($finish_table-&gt;execute()) {
     $finish_table-&gt;setFetchMode(PDO::FETCH_ASSOC);
  }
  
  while($row = $finish_table-&gt;fetch()) {  
  
      $vac_id = $row['vac_id'];   
      $vac_name = $row['vac_name'];   
      $vac_comp = $row['vac_comp'];   
      $full_name = $row['full_name']; 
  
  
      $mail = new PHPMailer;
      $mail-&gt;isSMTP();
      $mail-&gt;Host = '';
      $mail-&gt;SMTPAuth = true;
      $mail-&gt;SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
      $mail-&gt;Port = ;
      $mail-&gt;Username = '';
      $mail-&gt;Password = '';
      $mail-&gt;setFrom('', 'Information');
      $mail-&gt;addReplyTo('', ' Information');
      $mail-&gt;Subject = "Information";
  
      $mail-&gt;Body = strtr(file_get_contents('new_vac_email.html'), array('%full_name%' =&gt; $full_name, '%vac_id%' =&gt; $vac_id, '%vac_name%' =&gt; $vac_name, '%vac_comp%' =&gt; $vac_comp));
  
      //Same body for all messages, so set this before the sending loop
      //If you generate a different body for each recipient (e.g. you're using a templating system),
      //set it inside the loop
      //msgHTML also sets AltBody, but if you want a custom one, set it afterwards
      $mail-&gt;AltBody = 'To view the message, please use an HTML compatible email viewer!';
      //Connect to the database and select the recipients from your mailing list that have not yet been sent to
      //You'll need to alter this to match your database
      $mysql = mysqli_connect('host', 'user', 'pass');
      mysqli_select_db($mysql, 'db_name');
  
  
      $result = mysqli_query($mysql, "SELECT * FROM vac_mailing_system WHERE sent_status = false AND retry_attempts &lt; 5 LIMIT 0, 50");
      foreach ($result as $row) { //This iterator syntax only works in PHP 5.4+
  
  
          $mail-&gt;addAddress($row['email_address'], $full_name);
  
  
      if (!$mail-&gt;send()) {
          echo "Mailer Error (" . str_replace("@", "&amp;#64;", $row["email_address"]) . ') ' . $mail-&gt;ErrorInfo . '&lt;br /&gt;';
            //Mark it as sent in the DB
          mysqli_query(
              $mysql,
              "UPDATE vac_mailing_system SET sent_status = false, retry_attempts = +1 WHERE email_address = '" .
              mysqli_real_escape_string($mysql, $row['email_address']) . "'"
          );
  
          break; //Abandon sending
      } else {
          echo "Message sent to :" . $full_name . ' (' . str_replace("@", "&amp;#64;", $row['email_address']) . ')&lt;br /&gt;';
          //Mark it as sent in the DB
          mysqli_query(
              $mysql,
              "UPDATE vac_mailing_system SET sent_status = true WHERE email_address = '" .
              mysqli_real_escape_string($mysql, $full_name) . "'"
          );
      }
      // Clear all addresses and attachments for next loop
          $mail-&gt;clearAddresses();
          $mail-&gt;clearAttachments();
      }
  
      sleep(60);
      echo "emails sent";
  
      }
  </code></pre>