只有在向xml文件添加了打印功能后,PHPMailer才会发送重复的电子邮件


PHP Mailer sends a duplicate email only after adding functionality to print to an xml file

我有一个HTML表单,它提交给一个PHP文件来处理并通过PHP邮件发送电子邮件。

我已经搜索并可能花了一天多的时间阅读可能的解决方案(尝试singleto,启动新的邮件程序,清除收件人,检查是否有可能导致双重提交的不适当插件[如firefox的yslow]确保没有发生双重提交,在PHPmailer和其他一些程序中将格式更改为UTF-8。(

在所有这些之后,我把我的代码分解成几块,并一点一点地测试它,发现当我说要制作XML文件时,PHP邮递员决定重新发送电子邮件。(在一秒钟或更短的时间内(。

任何帮助都将不胜感激!!

这是我的代码(在XML之前(,只需将其转换为您的gmail密码:

  <?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->isSMTP();
  $mailer->Host = 'smtp.gmail.com';
  $mailer->Port = 587;
  $mailer->Username = $agent.'@gmail.com';
  $mailer->Password = 'Password';
  $mailer->SMTPSecure = 'tls';
  $mailer->SMTPAuth = true;

  $mailer->From = $agent.'@gmail.com';
  $mailer->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.<br />'n");
          $mailer->addAddress($rcp);
      }
      $mailer->Subject = $email_subject;
      $mailer->Body = $email_message;
      $mailer->isHTML(false);

      if(!$mailer->send()) {
         echo 'Message could not be sent to Ops. Go back and fix these errors then resubmit.';
         echo 'Mailer Error: ' . $mailer->ErrorInfo;
         exit;
      }
}
?>

HTML:

<form name="contactform" method="post" action="phpseg1.php">
<table width="600px">

<tr>

 <td valign="top">

  <label for="agent">Agent</label>

 </td>

 <td valign="top">
  <input  type="text" name="agent" maxlength="30" size="35">
 </td>
</tr>

<tr>

 <td valign="top">

  <label for="affiliate">Affiliate   </label>

 </td>

 <td valign="top">

  <select name="affiliate">
   <option value=""></option>
   <option value="CORP">CORP</option>
   <option value="(AA1008)">(AA1008)</option>
   <option value="(AA1012)">(AA1012)</option>
   <option value="(AA1014)">(AA1014)</option>
  </select>

 </td>


</tr> 

<tr>
<td valign="top">

  <label for="contract_number">Contract Number</label>

 </td>

 <td valign="top">

  <input  type="text" name="contract_number" maxlength="30" size="26">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="comments">Comments  </label>

 </td>

 <td valign="bottom">

  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

 </td>

</tr>



<tr>

 <td colspan="2" style="text-align:center">

  <input type="submit" value="Submit">   <a href="http://localhost/example/phpseg1.php"></a>

 </td>

</tr>



</tr>



</table>

</form>
<!-- include your own success html here -->

Email sent! 

通过在最后一个大括号之前添加以下内容,邮递员将发送2封电子邮件(为了测试起见,请确保您始终选择"Corp"作为附属公司,因为如果您选择另一个,则它应该发送两封电子邮件。(:

if ($mailer->send()) {
$str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
$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->reports = "";
$xml->reports->addChild('whensent', $whensent);
$xml->reports->addChild('whoto', $whoto);
$xml->reports->addChild('subject', $subject);
$xml->reports->addChild('emailmessage', $email_message);
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$path = "Test_Email_notices_sent/NO_NOTICE/NO_NOTICE";
$doc->save("{$path}_test.xml");
}
      if(!$mailer->send()) {
             echo 'Message could not be sent to Ops. Go back and fix these errors then resubmit.';
             echo 'Mailer Error: ' . $mailer->ErrorInfo;
             exit;
          }else{
//PUT ALL THE file creation code here
}

计划B

      if(!$mailer->send()) {
             echo 'Message could not be sent to Ops. Go back and fix these errors then resubmit.';
             echo 'Mailer Error: ' . $mailer->ErrorInfo;
             exit;
          }else{
$sent=TRUE;
}

然后更改

if ($mailer->send()) {

if($sent){