title: Is there a PHPmailer parameter to limit re-sends?
body: <p>When I use PHPmailer to send to a group of email addresses, it gets caught in a loop and keeps re-sending the email over and over (100 times or more).  It doesn't always do this however.  I can send to 10 or 12 addresses and it works fine.  But a larger list (25 addresses) seems to have trouble. I did find some non-existent email addresses in my list that rebound back to my "from" address mailbox. So I thought that might be the problem. But, I ran a test with just 3 addresses (one non-existent) and it worked as expected (good addresses received the mail, and the bad one was rebounded back to the sender mailbox).  No duplicate emails were sent.  </p>
  
  <p>I do use BCC to send my email, but I don't think that should matter.  I'm wondering if maybe there is some parameter I can set for PHPmailer that will tell it not to re-send any emails.  My web hosting site says it is not their server or mail system that is sending these emails, so I'm guessing it is something in PHPmailer. </p>
  
  <p>When my real-life use of PHPmailer doesn't work like it did when I tested it, that makes it very difficult to fix.  Anyone have any suggestions as to why my emails keep getting sent multiple times?  </p>
  
  <p>Also, I'd like to know if there's a way to stop PHPmailer, after $mail->Send() has been issued?  I'm getting a little embarrassed with the spam-like behavior of my emails (and my users are getting mad).  </p>
  
  <p>My basic code is below.  But, the PHP program code is the same in the cases where it works and doesn't work; it's just the input data that changes.  When it has worked, there were 2 smaller arrays:<br>
  emailistAry(1) = array(email1,email2,email3,...email10)  and<br>
  emailistAry(2) = array(email1,email2,email3,email4,email5).<br>
  When it keeps looping, there are 2 larger arrays:<br>
  emailistAry(1) = array(email1,email2,bademail1,email3,email4,bademail2,email5,...email25) and  emailistAry(2) = array(mail26,email27,email28,bademail3,email29,...email50).</p>
  
  <p>I haven't found the exact number that I can process successfully, because it would mean running tests that possibly fill people's mailboxes with endless multiple emails.  No one wants that. </p>
  
  <p>Here's my code: </p>
  
  <pre><code>&lt;?php session_start();
    // uses arrays of addresses to send emails to multiple people (with BCC)
    require '../PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer;
  ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html&gt;
   &lt;head&gt; 
   &lt;link href="../root.css" rel="stylesheet" type="text/CSS"/&gt;
   &lt;title&gt; Website Notification  &lt;/title&gt;
   &lt;meta http-equiv= "content-type" content="text/html; charset=iso-8859-1"/&gt;  
   &lt;/head&gt;
  
   &lt;body&gt;
   &lt;div style="top:0;padding:0;width:850px;margin-left:70px;"&gt;   
      &lt;?php
  
      //$emailistAry is gathered from addresses in a database.
      //       it contains 2 arrays of email arrays (multi-dimensional).
      //$emailist_cnt is determined from building above array (currently a 2) 
  
      $fromaddr = "myaddress@mywebsite.org";
      $toaddr = "groupaddress@mywebsite.com";
      $rplyaddr = "my2address@gmail.com";
      $subject = "--Special Notice--";
      $messageH = "&lt;p style='font-family:Arial,Helvetica,Georgia,\"Times New Roman\";font-size:90%;'&gt; 
          &lt;span style='font-size:120%;color:#336699;'&gt;Greetings HLCA Member!&lt;/span&gt; &lt;br/&gt;&lt;br/&gt;".  
          "This is my message text....".
          "&lt;/p&gt;";
      // Set mail environment variables
      $mail-&gt;isSMTP();                                   
      $mail-&gt;Host = 'smtp.mywebsite.org';                
      $mail-&gt;SMTPAuth = true;                            
      $mail-&gt;Username = $fromaddr;                   
      $mail-&gt;Password = "mypasswd";                       
      $mail-&gt;SMTPSecure = 'tls';                         
      $mail-&gt;Port = 587;                                
  
      // Set mail details that remain constant for every recipient
         $mail-&gt;SetFrom($fromaddr);      // mailbox you want email to come from
         $mail-&gt;AddAddress($toaddr);     // set recipient group (empty mailbox)
         $mail-&gt;AddReplyTo($rplyaddr);   // where reject msgs are sent
             $mail-&gt;Subject = make_goodemail($nly,'subj',$subject);
         $mail-&gt;Body    = $messageH;
  
         $goodlist_cnt = $emailist_cnt;
         $max_emails = 25;   // max num emails per list  
         $bcc_out_total = 0; //number emails output with BCC
         $badlist_cnt = 0; 
         $badlist = '';      // save emails that didn't get sent
         $email_badlist = array(); // store email lists that didn't get sent
         $duplist = "";
         $stop_err = false;
         $rcN = 0;  
  
      // Loop through email arrays and send one at a time (each contains an array of emails).
      // (Note that we do not use the 0 array positions. First entries are in position 1.)
  
         for ($i=1; $i &lt;= $emailist_cnt; $i+=1) { 
         echo "&lt;p style='margin-top:40px;font-size:120%;'&gt;&lt;b&gt; Processing list # ".$i."...&lt;/b&gt;&lt;/p&gt;"; 
         $duplist = "";
             for ($j=1; $j &lt;= $max_emails; $j+=1) {                  
             // add addresses from this list array to BCC parameter,  
             if ($emailistAry[$i][$j] != '') {
             // add current email to BCC for this list (save rc)
             $BCCresult = $mail-&gt;AddBCC($emailistAry[$i][$j]);
             if(!$BCCresult) {
                $duplist = $duplist.$emailistAry[$i][$j].", ";
             }
             }
             else {
               $j = $max_emails; //set as end of list
             }
             } // for j loop
  
         // Loop thru this BCC array and show list on screen. 
         $bccArray = $mail-&gt;GetBCCAddresses();
         $bcc_out = 0;  
         echo "&lt;br/&gt;Mail BCC list: ";
         for ($k=0;$k&lt;=$max_emails;$k+=1) {
                 if ($bccArray[$k][0] != '') {
            if ($k==0) {                   //first one
                       echo " ".$bccArray[$k][0]; } //no comma
            else {
                       echo ", ".$bccArray[$k][0]; }
            $bcc_out += 1;
                 }
             else {
                $k=$max_emails;
                 }
         }
         // $bcc_out_total = $bcc_out_total + $bcc_out;
         echo "&lt;br/&gt;BCC count = ".$bcc_out;
         if ($duplist != "") { //print duplicate list  
            echo "&lt;br/&gt; &gt;&gt;Duplicate or invalid emails not included: ".$duplist; 
         }
             //echo "&lt;br/&gt;&lt;br/&gt;would send email list here.  webpass=".$webpass;
  
         // **********  ***********  *************   ************
             // SEND THE MAIL with current BCC list of recipients.
         // Output message indicating if mail for this group was successfully or not. 
  
         if (!$mail-&gt;Send()) { // Send mail, else save emailist.
             // see if connection error, and process accordingly 
             $mail_err = $mail-&gt;ErrorInfo;
             $pos = strpos($mail_err,'SMTP connect() failed'); 
             if ($pos===true || $pos &gt;= 0) {
                 $addmsg = "(Probably a password error. Try again.) &lt;br/&gt;";
                 if ($i == 1) {        // connect error on first try, so 
                    $stop_err = true;  // stop processing cuz this error will continue
                    $rcN = 2;
                 }      
             }
             else { 
             $addmsg = ""; 
                 $rcN = 1;  //set general error for return
             }
                 echo "&lt;h4 style='color:red;margin-top:20px;margin-bottom:5px;text-align:center;'&gt;
                       Problem sending emails. ".$addmsg." Mailer Error: ".$mail-&gt;ErrorInfo."&lt;/h4&gt;";
             echo "&lt;h4 style='color:blue;margin-top:10px;margin-left:20px;margin-bottom:0px;'&gt;Email List #".$i." not sent. &lt;/h4&gt;";
  
             if (!$stop_err) { //other error, so handle and keep going.
                 $goodlist_cnt = $goodlist_cnt - 1;  
              //save list in array (as one long string). 
                 $badlist_cnt+=1;
                 $email_badlist[$badlist_cnt] = implode(",",$emailistAry[$i]); //save as a string
              }
         } //if !Mail-send
  
         else { // Successful email
             $bcc_out_total = $bcc_out_total + $bcc_out;
             echo "&lt;h4 style='color:blue;margin-top:20px;font-weight:bold;'&gt; 
                   Email group ".$i." sent successfully!  &lt;/h4&gt;";
         }
  
         // Clear cumulative recipient list for next loop
         $mail-&gt;clearBCCs();
  
         if ($stop_err) {
             $i = $emailist_cnt; //set to last list so loop will stop
         }
  
         } // for i loop
  
         if ($badlist_cnt &gt; 0)  { // error, but not stop_err, so lists are saved elsewhere.
             $badlist_txt = "&lt;span style='color:red;font-size:80%;'&gt; (".$badlist_cnt." list(s) not sent)&lt;/span&gt;";
         //write badlists out to file for fixing and later processing
         include ('includes/memNotifyErrSave.php'); 
         }
         else {
         $badlist_txt = '';
         }
  
         if ($stop_err) {
         echo "&lt;br/&gt; &lt;h4 style='color:red;margin-top:50px;text-align:center;'&gt; Processing stopped due to serious error!&amp;nbsp; ".
                     "If password error, try again with correct password.&lt;br/&gt;&lt;br/&gt;".
                 "&lt;span style='font-size:120%;color:blue;font-weight:bold;'&gt;* No emails were sent * &lt;/span&gt;&lt;/h4&gt;";
         }
         else {
             echo "&lt;h4 style='color:blue;font-size:125%;font-weight:bold;margin-top:40px;'&gt;
               Sent this notice to ".$bcc_out_total." unique address(es), using ".$goodlist_cnt." email(s)".$badlist_txt.".&lt;/h4&gt;";
         }
  
         // code for exit/return buttons
  
      // Clear remaining cumulative lists
      $mail-&gt;clearReplyTos();
      $mail-&gt;clearAddresses();  
      ?&gt;
  
    &lt;/div&gt;   
  
  &lt;/body&gt;
  
  &lt;/html&gt; 
  </code></pre>