title: Sending multiple emails with PHPmailer
body: <p><em>Edit: I forgot I'd created the <code>SendMail();</code> function myself, which is why the explanation doesn't mention at first what it does.</em></p>
  
  <p>I'm having some trouble with PHPMailer (<a href="https://github.com/PHPMailer/PHPMailer" rel="nofollow">https://github.com/PHPMailer/PHPMailer</a>) when attempting to send two emails, one directly after the other.</p>
  
  <p>The script is almost completely 'out of the box', with only a few modifications such as a <code>foreach</code> loop to allow for multiple addresses, and everything still works perfectly.</p>
  
  <p>However, if I attempt to call more than one instance of <code>SendMail();</code> I get the error message:</p>
  
  <pre><code>Fatal error: Cannot override final method Exception::__clone() in .... online 0
  </code></pre>
  
  <p>Previously I was using the in-built <code>mail();</code> function, which allowed me to use it as many times as I liked in quick succession , but it doesn't appear to be that simple with PHPmailer:</p>
  
  <pre><code>$to = me@me.com;
  $to2 = me2@me2.com';
  $headers = 'php headers etc';
  $subject = 'generic subject';
  $message = 'generic message';
  mail($to, $subject, $message, $headers);
  mail($to2, $subject, $message, $headers);
  </code></pre>
  
  <p>The above would result in two identical emails being sent to different people, however I can't easily replicate this functionality with PHPmailer.</p>
  
  <p>Is there a way of stacking these requests so that I can send successive emails without it failing?  Forcing the script to wait until the first email has been sent would also be acceptable, although not preferential.</p>
  
  <p>As I mentioned I know it works when only one instance is called, but I don't seem to be able to re-use the function.</p>
  
  <p>I haven't included the source code, although it is all available on the link provided above.</p>
  
  <p>Thanks in advance</p>
  
  <p><strong>Edit as requested</strong></p>
  
  <pre><code>// First Email
  $to = array(
  'test@test.com',
   'test2@test.com',);
  $subject = "Subject";
  $message = $message_start.$message_ONE.$message_end;
  
  sendMail();
  
  // Second Email
  $to = array(
  'test@test.com',
   'test2@test.com',);
  $subject = "Subject";
  $message = $message_start.$message_TWO.$message_end;
  
  sendMail();
  </code></pre>
  
  <p>The above is how I want this to work, as it would work with <code>mail();</code>.  The first email will work fine, the second will not.</p>
  
  <p><strong>SendMail() code</strong></p>
  
  <p>This is from the PHPmailer website, and is what is defined as <code>SendMail();</code>.  The only difference from the example is the loop for <code>AddAddress</code>, and the inclusion of <code>$to</code> as a global variable.</p>
  
  <pre><code>$mail = new PHPMailer();
  
  $mail-&gt;IsSMTP();                                      // set mailer to use SMTP
  $mail-&gt;Host = "smtp1.example.com;smtp2.example.com";  // specify main and backup server
  $mail-&gt;SMTPAuth = true;     // turn on SMTP authentication
  $mail-&gt;Username = "jswan";  // SMTP username
  $mail-&gt;Password = "secret"; // SMTP password
  
  $mail-&gt;From = "from@example.com";
  $mail-&gt;FromName = "Mailer";
  foreach($to as $to_add){
  $mail-&gt;AddAddress($to_add);                  // name is optional
  }
  $mail-&gt;AddReplyTo("info@example.com", "Information");
  
  $mail-&gt;WordWrap = 50;                                 // set word wrap to 50 characters
  $mail-&gt;AddAttachment("/var/tmp/file.tar.gz");         // add attachments
  $mail-&gt;AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
  $mail-&gt;IsHTML(true);                                  // set email format to HTML
  
  $mail-&gt;Subject = "Here is the subject";
  $mail-&gt;Body    = "This is the HTML message body &lt;b&gt;in bold!&lt;/b&gt;";
  $mail-&gt;AltBody = "This is the body in plain text for non-HTML mail clients";
  
  if(!$mail-&gt;Send())
  {
     echo "Message could not be sent. &lt;p&gt;";
     echo "Mailer Error: " . $mail-&gt;ErrorInfo;
     exit;
  }
  
  echo "Message has been sent";
  </code></pre>