ajax - wp_mail doesn't work when logged in?

admin2025-06-03  8

I am having trouble with a wp_mail function I created, which sends an email when I press a button. It does work great when you do this logged out. But as soon as I am logged in it stops working. It is not a capability issue, as I get the error as a Super Admin also.

Why would this happen? I am using a PHP function together with Ajax to send the email, Javascript function is called onclick.

function searchEmail(email,title,content,location) {
  var admin_url = admin_ajax.ajaxurl;
  $.ajax({
    type: "POST",
    url: admin_url,
    datatype: "html",
    data: { 'action': 'search_notify_email', email: email, title: title, content: content, location: location },
    success: function() {
      searchNotification();
    },error:function() {
      searchNotificationError();
    }
  });
}

PHP:

function search_notify_email() {
  // Set variables
  $email = $_POST['email'];
  $title = $_POST['title'];
  $content = $_POST['content'];
  $location = $_POST['location'];
  // Change Email to HTML
  add_filter( 'wp_mail_content_type', 'set_email_content_type' );
    $to = $email;
    $subject = "Test subject!";
    $message = "<img src='favicon.png'><br><b>Test!</b>";
    if (empty($title)) {
      $message .= "<br><br><b>" . $_POST['content'] . "</b> test.<br> ";
    }
    else {
      $message .= "<br><br><b>" . $_POST['content'] . "</b> test " . $_POST['title'] . " test.<br> ";
    }
    if (!empty($location)) {
      $message .= "Test <b>" . $_POST['location'] . "</b>";
    }
    $headers[] = 'From: Testing <[email protected]>';

    if ( wp_mail($to, $subject, $message, $headers) ) {
      // Success
    } else {
      // Error
    }
    die();
    // Remove filter HTML content type
    remove_filter( 'wp_mail_content_type', 'set_email_content_type' );
}
add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_emaill', 'search_notify_email');

// Reset Email content type to standard text/html
function set_email_content_type() {
    return 'text/html';
}

I am having trouble with a wp_mail function I created, which sends an email when I press a button. It does work great when you do this logged out. But as soon as I am logged in it stops working. It is not a capability issue, as I get the error as a Super Admin also.

Why would this happen? I am using a PHP function together with Ajax to send the email, Javascript function is called onclick.

function searchEmail(email,title,content,location) {
  var admin_url = admin_ajax.ajaxurl;
  $.ajax({
    type: "POST",
    url: admin_url,
    datatype: "html",
    data: { 'action': 'search_notify_email', email: email, title: title, content: content, location: location },
    success: function() {
      searchNotification();
    },error:function() {
      searchNotificationError();
    }
  });
}

PHP:

function search_notify_email() {
  // Set variables
  $email = $_POST['email'];
  $title = $_POST['title'];
  $content = $_POST['content'];
  $location = $_POST['location'];
  // Change Email to HTML
  add_filter( 'wp_mail_content_type', 'set_email_content_type' );
    $to = $email;
    $subject = "Test subject!";
    $message = "<img src='favicon.png'><br><b>Test!</b>";
    if (empty($title)) {
      $message .= "<br><br><b>" . $_POST['content'] . "</b> test.<br> ";
    }
    else {
      $message .= "<br><br><b>" . $_POST['content'] . "</b> test " . $_POST['title'] . " test.<br> ";
    }
    if (!empty($location)) {
      $message .= "Test <b>" . $_POST['location'] . "</b>";
    }
    $headers[] = 'From: Testing <[email protected]>';

    if ( wp_mail($to, $subject, $message, $headers) ) {
      // Success
    } else {
      // Error
    }
    die();
    // Remove filter HTML content type
    remove_filter( 'wp_mail_content_type', 'set_email_content_type' );
}
add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_emaill', 'search_notify_email');

// Reset Email content type to standard text/html
function set_email_content_type() {
    return 'text/html';
}
Share Improve this question edited Feb 14, 2019 at 13:59 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Feb 14, 2019 at 13:44 joq3joq3 3813 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You've got a typo in the hook that runs for logged in users:

add_action('wp_ajax_search_notify_emaill', 'search_notify_email');

There's an extra l in the hook name. The hook names need to be the same, apart from nopriv_, so you should have:

add_action('wp_ajax_nopriv_search_notify_email', 'search_notify_email');
add_action('wp_ajax_search_notify_email', 'search_notify_email');

When you send an AJAX request with an action WordPress runs the hook wp_ajax_{$action} (where $action is the parameter passed with the request) if you're logged in, or wp_ajax_nopriv_{$action} if the user is not logged in. Since you'd used an incorrect hook name for the logged-in version (due to the typo) your search_notify_email() function wasn't hooked to run when the user was logged in.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748907780a314715.html

最新回复(0)