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';
}