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]" . "\r\n";

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

And AJAX Code is as follow

function my_scripts(){
    <script type="text/javascript">

     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) {
        jQuery(".confirm_email_section").html("<span class='alert alert-success'>Confirmation link sent to your email. Check your email<span>");

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


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 2


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

In your JS you fill your data like so:

var data = {

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 ;)


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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