0

I can't figure out for the life of me why this is sending twice. It's not in any loop, and I have an if statement checking if isset but nothing I do stops it from sending twice.

It's a basic form that has 4 fields but data is extracted based on one field and email sent out based on the email field.

Everytime I submit the page it sends it twice.

   //Email Function
        if(isset($_GET['email'])){

             if($_GET['employee'] == '1') {
                    $pp = 'Plan Participant (Employee)';
                } else if($_GET['employee'] == '2') {
                    $pp = 'Plan Participant (Employer)';
                }
            //$headers = 'From: americasbest401k.com' . "\r\n";
            $company = '<br><br>Company Name: ' . $co_name ;
            $plan    = '<br><br>Plan Name: ' . $plan_name;
            $headers = 'From: Fee Checker Page <Americasbest401k.com>' . "\r\n";
            $message = $_GET['name_field'] . ' used the fee checker<br><br>Email: ' . $_GET['email'] . '<br><br>Participant or Sponsor: ' . $pp . '' . $company . ' ' . $plan;
            wp_mail('support@americasbest401k.com', '401k Fee Checker', $message, $headers);

            //email to user 
            $headers = 'From: '.get_post_meta($post->ID,'from-email',true). "\r\n";
            $message = wpautop( get_post_meta($post->ID,'employer-email',true)).' <a href="'.  site_url().'/fee-checker/?reffered=yes&company='.  $_GET['company'].'"><strong>'.get_post_meta($post->ID,'fee-link-text',true).'</strong></a>';

            wp_mail($_GET['email'], "Take Action on Your 401k!", $message, $headers);

        }
  • 1
    The function is probably being called twice for some reason, can you declare a global php variable $testemail = 1; and tag it on the end of the subject line: "Take Action on Your 401k! " . $testemail then increment it by 1 after the wp_mail function: $testemail++; If the email subject lines have 1,2 after them then the code is being called multiple times. – Jason Murray Mar 1 '15 at 19:56
  • @Jason That was a excellent suggestion. I have run into similar situations before where doing that would have been a great way to diagnose the problem. – skribe Mar 1 '15 at 20:28
  • If you just use the normal php mail() function what happens? – skribe Mar 1 '15 at 20:31
3

Thanks for the comments!

I figured it out. I tried the regular php mail function and still the same thing. Then I decided to make a page template and add only the mail() function, get_header(), and get_footer(). It still sent it out twice. So I opened my header.php and commented the wp_head() function to rule out my functions and any included files in the theme. Still it was sending it twice. So I removed the header all together and it sent the email once.

After narrowing it down it turned out to be the link to favicon. In the theme there is an option to upload a custom favicon. In the header it had the code to echo out the link to favicon but I hadn't uploaded it yet so it was empty. For some strange reason this was causing my emails to be sent out twice. I'm thinking it had something to do with the headers maybe I'm not sure but I changed the code to only display if it wasn't empty. Like this:

<?php if(of_get_option( 'favicon' )):?>
  <link href="<?php echo of_get_option( 'favicon' ); ?>" rel="shortcut icon">
<?php endif; ?>

If you are setting meta data or links in between the head section make sure you include and if like above or you just might drive someone crazy over something so small.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.