wp_mail() not sending emails with ajax

Question

I am using wp_mail() to send email using ajax. My mail code is as follow.

add_action( 'wp_ajax_send_confirmation_email', 'wpse_sendmail' );
add_action( 'wp_ajax_nopriv_send_confirmation_email', 'wpse_sendmail' );

function wpse_sendmail() {
 if(isset($_GET['is_email']) && $_GET['is_email'] == true){
        $temp_dir   = get_template_directory_uri();

        $url        = $temp_dir."/confirm_email.php?id=".$_REQUEST['id']."&key=".$_REQUEST['key'];
        $message    = "Username:".$_REQUEST['name']."Click on below link to confirm your email;".$url;

        $subject    = "Email confirmation Link";
        $headers    = "From: wordpress@property-penang.com" . "rn";

        if ( wp_mail( $_REQUEST['email'], $subject, $message, $headers ) ) {
            echo "1";
        } else {
            echo "0";
        }
    }
}

And AJAX Code is as follow

add_action('wp_footer','my_scripts');
function my_scripts(){
    ?>
    <script type="text/javascript">
    jQuery("#send").click(function(e){

     e.preventDefault(); // if the clicked element is a link

    //...
      var confirm_email=jQuery("#confirm_email").val();
          var confirm_key=jQuery("#confirm_key").val();
          var user_email='<?php echo $current_user->user_email;?>';
          var display_name='<?php echo $current_user->display_name;?>';
          var user_id='<?php echo $current_user->ID;?>';


    var data = { 'action':'send_confirmation_email', 'email':'confirm_email','key':'confirm_key','name':'display_name','id':'user_id','is_email':'true' };

    jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
        if(response=="1"){
        jQuery(".confirm_email_section").html("<span class='alert alert-success'>Confirmation link sent to your email. Check your email<span>");

        }
        else{
        jQuery(".confirm_email_section").html("<span class='alert alert-danger'>Error Occured<span>");
        }
    });

});
    </script>
    <?php
}

Every parameter passed with ajax are receiving into above wpse_sendmail() function but email is not sending. It always returns false. And email is also not sending with mail() function in functions.php but working within a custom file. I don’t know that what is going wrong. If any one help me, i will be thankful.

0
, Mohd Hasan 3 years 2018-05-13T12:42:39-05:00 0 Answers 87 views 0

Leave an answer

Browse
Browse