• Resolved loopforever

    (@loopforever)


    Hello,
    With wp mail, I’m trying to get data from a form based on the variable id address and send mail. I do this in the pop-up window that opens after pressing a button.

    If there is a post and the verify nonce is true, I am sending the data from this form. So far, so good.
    However, I cannot show the user whether the mail was successful or not. According to this document, wp mail returns true or false. I tried to proceed from here. However, the strange thing is that even when I don’t send mail, wp_mail returns false. That is, it shows false even if the submit button is not pressed yet. However, if the mail is successful, there is no problem for this. I see that it was successful after the mail was sent and the page loaded.

    Also, to reiterate, I don’t use Ajax. After clicking the Submit button, the page reloads.

    My Code is:

     <?php
    //...
     if (!empty($_POST) && wp_verify_nonce($_POST['mail_control'], 'mail_control'))
    {
    //...
     if(!get_post_meta($order_id, 'reject_email', true)){     
                    update_post_meta($order_id, 'reject_email', true);
                    $to = $recipient_mails;
                    $result_for_reject = '';
                    $subject = 'Rejected';
                    $body = $message;
                    $headers = array('Content-Type: text/html; charset=UTF-8','From:MyWebSite <[email protected]>');
                    $result_for_reject = wp_mail($to, $subject, $body, $headers);
    
                }
    }
    //...
    //Pop-up window finished 
    <?php 
        //Check email is sent or not sent
        if($result_for_rma_reject){
        echo '<div class="woocommerce-message " role="alert">Successful.</div>';
        }
        if($result_for_rma_reject==false) { 
        echo '<div class="woocommerce-error " role="alert">Unsuccessful.</div>';
         
       }
    ?>
    • This topic was modified 2 years, 6 months ago by loopforever.
Viewing 4 replies - 1 through 4 (of 4 total)
  • You need to set the variable earlier and not inside an IF conditional, that way if it doesn’t go to IF you’ll have an undefined variable. Please try with the code below:

    //...
    $result_for_reject = false; // default value
    
    if (!empty($_POST) && wp_verify_nonce($_POST['mail_control'], 'mail_control')) {
    //...
        if(!get_post_meta($order_id, 'reject_email', true)){     
            update_post_meta($order_id, 'reject_email', true);
            $to = $recipient_mails;
            $subject = 'Rejected';
            $body = $message;
            $headers = array('Content-Type: text/html; charset=UTF-8','From:MyWebSite <[email protected]>');
            $result_for_reject = wp_mail($to, $subject, $body, $headers); // when sent should be true
        }
    }
    //...
    //Pop-up window finished 
    
    //Check email is sent or not sent
    if($result_for_reject){
        echo '<div class="woocommerce-message " role="alert">Successful.</div>';
    }else{
        echo '<div class="woocommerce-error " role="alert">Unsuccessful.</div>';
    }
    Thread Starter loopforever

    (@loopforever)

    Thank you for your response.

    I did these but it didn’t fix the problem. Actually, I’ve tried this before.

    The problem still continues. Although the form is not submitted, it sees it as wrong.
    Of course I also changed this false value to a different value. The result is the same.

    Dion

    (@diondesigns)

    Try this instead:

    //...
    $result_for_reject = ''; // default value
    
    if (!empty($_POST) && wp_verify_nonce($_POST['mail_control'], 'mail_control')) {
    //...
        if(!get_post_meta($order_id, 'reject_email', true)){     
            update_post_meta($order_id, 'reject_email', true);
            $to = $recipient_mails;
            $subject = 'Rejected';
            $body = $message;
            $headers = array('Content-Type: text/html; charset=UTF-8','From:MyWebSite <[email protected]>');
            $result_for_reject = wp_mail($to, $subject, $body, $headers); // when sent should be true
        }
    }
    //...
    //Pop-up window finished 
    
    //Check email is sent or not sent
    if($result_for_reject){
        echo '<div class="woocommerce-message " role="alert">Successful.</div>';
    }else if($result_for_reject === false){
        echo '<div class="woocommerce-error " role="alert">Unsuccessful.</div>';
    }
    Thread Starter loopforever

    (@loopforever)

    else if … Big mistake 🙂
    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Wp Mail Success or Failure Issue’ is closed to new replies.