title: PHPMailer: Handling "recipients failed"
body: <p>I'm sending e-mails via PHPMailer in a loop but if for some reason I send to an address not accepted by the receiving server PHPMailer throws an exception and the script dies. I would like my script to continue and handle this bad address in another way.</p>
  
  <p>Any ideas on how to avoid PHPMailer dying on exceptions?</p>
  
  <pre><code>try
  {
      // prep
      $mail-&gt;IsSMTP();
      $mail-&gt;SMTPKeepAlive = true;
      $mail-&gt;SMTPDebug = 0;
      $mail-&gt;SetFrom(email_reply_adress, email_from_name);
      $mail-&gt;Host = email_smtp_host;
      $mail-&gt;SMTPAuth = true; 
      $mail-&gt;Username = email_username; 
      $mail-&gt;Password = email_password;
      $mail-&gt;IsHTML(true);
      $mail-&gt;AddReplyTo(email_reply_adress, email_from_name);
  
      //
      $query = "SELECT * FROM table WHERE sent = '0'";
      $result = @mysql_query($query);
      while($row = @mysql_fetch_array($result))
      {
          // receiver
          $mail-&gt;AddAddress($row['email']);
  
          // subject
          $mail-&gt;Subject = utf8_decode($row['subject']);
  
          // build mail
          $mail-&gt;Body = utf8_decode(stripslashes($row['content']));
  
          // send it                  
          if($mail-&gt;Send())
          {
              // stats
              $sent = "UPDATE table SET sentdate = now(), sent = '1' WHERE id = '".$row['id']."'";
              mysql_query($sent);
          }
  
          // clean up lists
          $mail-&gt;ClearAllRecipients();
      }
  }
  catch(phpmailerException $e)
  {
      echo $e-&gt;errorMessage(); //Pretty error messages from PHPMailer
  }
  </code></pre>