TransWikia.com

wp_mail() not sending emails with ajax

WordPress Development Asked by Mohd Hasan on January 16, 2021

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: [email protected]" . "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.

2 Answers

please check add_action( 'wp_ajax_send_confirmation_email', 'wpse_sendmail' ); it should be add_action( 'wp_ajax_wpse_sendmail', 'wpse_sendmail' ); same as your function name

Answered by Manan on January 16, 2021

And there is no way it will work this way...

In your JS you fill your data like so:

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

And later, in your AJAX callback in PHP you use it as follows:

wp_mail( $_REQUEST['email'], $subject, $message, $headers );

So you're trying to send email message using "confirm_email" as recipient address. It can't and it won't work ;)

Answered by Krzysiek Dróżdż on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP