简体   繁体   中英

PHP email is sending twice when form is submitted

I set up a custom price quote feature that sends the user an email when they click submit. The issue I'm having is that even if you hit 'Submit' once, it always sends two emails. I'm using this on aa WordPress site and this code is part of the price quote plugin that I wrote.

I set up a variable called $testemail that is set to 1, and is supposed to increment by 1 when the wp_mail function is called, but both emails are still showing just the number 1 so the second email doesn't increment it at all.

Here's my code that is sending the email:

$email_array = array($get_option_array['franchise_email'], 
$email_address_sanitize, "info@example.com");
$subject = "Price Quote Submission";
$to = $email_array;
  if (isset($_POST['submit-clicked'])) {
      if ($check_human_clean != 2) {
        echo '<div>';
        echo '<p class="error">Human verfication is incorrect. Please try again.</p>';
        echo '</div>';
      }
      else {
        if (wp_mail($to, $subject, $message)) {
          echo '<div id="dialog" title="Price Quote Submitted">';
          echo 'div id="popmake-233">';
          echo '<p>Your price quote has been sent successfuly. We\'ll be in touch shortly. If you don\'t hear from us, please give us a call directly at (909) 982-9999. Thank you!</p>';
          echo '</div>';
          $testemail++;
        }
        else {
          echo '<p class="error">An error occured while processing your price quote. Please contact the franchise owner at ';
          echo $get_option_array['franchise_phone'];
          echo '</p>';
        }
      }
    }

Everything I've read is that the code must be run twice, but my $testemail variable isn't incrementing so I'm not sure that's the case. Also, I'm using SendGrid but I already tried turning off SendGrid and that didn't solve the issue. Other than that, it's just using the wp_mail default function, which I believe uses PHP Mailer.

I'm a junior developer so I'm sure I'm just overlooking something, but I'm stumped at this point. Any help would be great, thanks.

If $testemail is not being incremented, it's possible that something in WP is running this function twice, separately, and therefore $testemail as a local variable will not increment on the second run.

Here are a few possible workarounds:

1) You could consider making $testemail a global variable.

2) When the email is successfully sent, store that fact in a database. Prior to sending the email, check the database see if the email has been sent. If so, do not send the email again.

3) Look deeper into why WP is calling this function twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM