title: PHP Mailer sends a duplicate email only after adding functionality to print to an xml file
body: <p>I have an HTML form that submits to a PHP file to process and send out emails via PHP mailer.</p>
  
  <p>I have searched and probably spent over a days work reading through possible solutions (tried singleto, starting new mailers, clearing recipients, checking for inappropriate plugins that would cause double submission [such as yslow for firefox] made sure that double submission was not happening, changing formatting to UTF-8 in the PHPmailer and some others.)</p>
  
  <p>After all that I broke my code into bits and tested it bit by bit and found that it was when I stated to make XML files was when the PHP mailer decides to resend the email. (within a second or less).</p>
  
  <p>Any help at all would be greatly appreciated!! </p>
  
  <p>Here is my code (before XML) just switch it around to have your gmail password:</p>
  
  <pre><code>  &lt;?php
      require 'PHPMailer-master/PHPMailerAutoload.php';
  
      if(isset($_POST['contract_number'])) {
  
          // Who to send email to and subject
           function clean_string($string) {
            $bad = array("content-type","bcc:","to:","cc:","href");
            return str_replace($bad,"",$string);
          }
  
           // TEST EMAIL FIELDS //
  
           $base_recipients = array('test@gmail.com');
  
           $pre_30_cancel_recipients = array('test1@gmail.com');
  
           $not_aa1014_recipients = array('test2@gmail.com');
  
           $aa1014_recipients = array('test3@gmail.com');
  
           // TEST EMAIL FIELDS //
  
  
  
           // $base_recipients = array('test5@gmail.com', 'test6@gmail.com', 'test7@gmail.com');
  
           // $pre_30_cancel_recipients = array('test8@gmail.com');
  
           // $not_aa1014_recipients = array('test9@gmail.com');
  
           // $aa1014_recipients = array('test10@gmail.com');
  
  
  
          $agent = $_POST['agent']; 
          $contract_number = $_POST['contract_number']; 
          $comments = $_POST['comments']; 
          $affiliate = $_POST['affiliate']; 
          $email_subject = $_POST['comments']; 
  
        $email_message = "Form details below.\n\n";
  
    $email_message .= "Comments: ".clean_string($comments)."\n";
  
  
  
    $mailer = new PHPMailer;
  
  
  
    $mailer-&gt;isSMTP();
  
    $mailer-&gt;Host = 'smtp.gmail.com';
  
    $mailer-&gt;Port = 587;
  
    $mailer-&gt;Username = $agent.'@gmail.com';
  
    $mailer-&gt;Password = 'Password';
  
    $mailer-&gt;SMTPSecure = 'tls';
  
    $mailer-&gt;SMTPAuth = true;
  
  
  
    $mailer-&gt;From = $agent.'@gmail.com';
  
    $mailer-&gt;FromName = $agent;
  
        $final_recipients = array_merge($base_recipients);
  
        if ($affiliate == '(AA1014)') {
            $final_recipients = array_merge($final_recipients,$aa1014_recipients);
        }
        if ($affiliate == '(AA1008)') {
            $final_recipients = array_merge($final_recipients,$not_aa1014_recipients);
        }
        if ($affiliate == '(AA1012)') {
            $final_recipients = array_merge($final_recipients,$not_aa1014_recipients);
        } else {
            $final_recipients = array_merge($final_recipients);
        }
  
        foreach ($final_recipients as $rcp) {
            echo("Adding ".$rcp." to recipient list.&lt;br /&gt;\n");
            $mailer-&gt;addAddress($rcp);
        }
  
        $mailer-&gt;Subject = $email_subject;
        $mailer-&gt;Body = $email_message;
        $mailer-&gt;isHTML(false);
  
  
        if(!$mailer-&gt;send()) {
           echo 'Message could not be sent to Ops. Go back and fix these errors then resubmit.';
           echo 'Mailer Error: ' . $mailer-&gt;ErrorInfo;
           exit;
        }
  }
  ?&gt;
  </code></pre>
  
  <p>HTML: </p>
  
  <pre><code>&lt;form name="contactform" method="post" action="phpseg1.php"&gt;
  
  &lt;table width="600px"&gt;
  
  
  
  &lt;tr&gt;
  
  
  
   &lt;td valign="top"&gt;
  
  
  
    &lt;label for="agent"&gt;Agent&lt;/label&gt;
  
  
  
   &lt;/td&gt;
  
  
  
   &lt;td valign="top"&gt;
  
    &lt;input  type="text" name="agent" maxlength="30" size="35"&gt;
  
   &lt;/td&gt;
  
  &lt;/tr&gt;
  
  
  
  &lt;tr&gt;
  
  
  
   &lt;td valign="top"&gt;
  
  
  
    &lt;label for="affiliate"&gt;Affiliate   &lt;/label&gt;
  
  
  
   &lt;/td&gt;
  
  
  
   &lt;td valign="top"&gt;
  
  
  
    &lt;select name="affiliate"&gt;
  
     &lt;option value=""&gt;&lt;/option&gt;
  
     &lt;option value="CORP"&gt;CORP&lt;/option&gt;
  
     &lt;option value="(AA1008)"&gt;(AA1008)&lt;/option&gt;
  
     &lt;option value="(AA1012)"&gt;(AA1012)&lt;/option&gt;
  
     &lt;option value="(AA1014)"&gt;(AA1014)&lt;/option&gt;
  
    &lt;/select&gt;
  
  
  
   &lt;/td&gt;
  
  
  
  
  
  &lt;/tr&gt; 
  
  
  
  &lt;tr&gt;
  
  &lt;td valign="top"&gt;
  
  
  
    &lt;label for="contract_number"&gt;Contract Number&lt;/label&gt;
  
  
  
   &lt;/td&gt;
  
  
  
   &lt;td valign="top"&gt;
  
  
  
    &lt;input  type="text" name="contract_number" maxlength="30" size="26"&gt;
  
  
  
   &lt;/td&gt;
  
  
  
  &lt;/tr&gt;
  
  
  
  &lt;tr&gt;
  
  
  
   &lt;td valign="top"&gt;
  
  
  
    &lt;label for="comments"&gt;Comments  &lt;/label&gt;
  
  
  
   &lt;/td&gt;
  
  
  
   &lt;td valign="bottom"&gt;
  
  
  
    &lt;textarea  name="comments" maxlength="1000" cols="25" rows="6"&gt;&lt;/textarea&gt;
  
  
  
   &lt;/td&gt;
  
  
  
  &lt;/tr&gt;
  
  
  
  
  
  
  
  &lt;tr&gt;
  
  
  
   &lt;td colspan="2" style="text-align:center"&gt;
  
  
  
    &lt;input type="submit" value="Submit"&gt;   &lt;a href="http://localhost/example/phpseg1.php"&gt;&lt;/a&gt;
  
  
  
   &lt;/td&gt;
  
  
  
  &lt;/tr&gt;
  
  
  
  
  
  
  
  &lt;/tr&gt;
  
  
  
  
  
  
  
  &lt;/table&gt;
  
  
  
  &lt;/form&gt;
  
  &lt;!-- include your own success html here --&gt;
  
  
  
  Email sent! 
  </code></pre>
  
  <p>By adding the following before the last curly brace the mailer will send 2 emails (For the test sake make sure you are always selecting "Corp" as affiliate because if you select another then it should send two emails.): </p>
  
  <pre><code>if ($mailer-&gt;send()) {
  
  $str = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;entrys&gt;&lt;/entrys&gt;';
  $final_recipients = implode (',', $final_recipients);
  $xml = simplexml_load_string($str);
  
  $whensent = date("Y-m-d H:i:s");
  $whoto = $final_recipients;
  $subject = $email_subject;
  
  $xml-&gt;reports = "";
  $xml-&gt;reports-&gt;addChild('whensent', $whensent);
  $xml-&gt;reports-&gt;addChild('whoto', $whoto);
  $xml-&gt;reports-&gt;addChild('subject', $subject);
  $xml-&gt;reports-&gt;addChild('emailmessage', $email_message);
  
  $doc = new DOMDocument('1.0');
  $doc-&gt;formatOutput = true;
  $doc-&gt;preserveWhiteSpace = true;
  $doc-&gt;loadXML($xml-&gt;asXML(), LIBXML_NOBLANKS);
  $path = "Test_Email_notices_sent/NO_NOTICE/NO_NOTICE";
  
  $doc-&gt;save("{$path}_test.xml");
  
  }
  </code></pre>