I created a function which I use to send an email when a specific button is pressed. This works great, except that it always ends up in spam instead of the inbox.

This is the function:

function search_notify_email() {
  // Set variables
  $email = $_POST['email'];
  $title = $_POST['title'];
  $content = $_POST['content'];
  $location = $_POST['location'];
  $siteurl = $_POST['siteurl'];
  $networkurl = network_site_url();
  $themeurl = get_stylesheet_directory_uri();
  // Call Change Email to HTML function
  add_filter( 'wp_mail_content_type', 'set_email_html_content_type' );
    $to = $email;
    $subject = "Attention: Test!";
    $message = "<html>
    $headers[] = 'From: Example <[email protected]>';

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

I have other emails sent on my site with the same email address used and these do not end up in the spam folder.

Any idea why this happens?

2 Answers 2


There are a wide variety of things this could be, so any answers to this question would be speculation. Keep in mind, it's the email that gets marked as spam (and the details of the sender, domain, etc), not the code that generates the email.

That being said, I would investigate the sender information first to make sure the headers in the email are set to show the email is sent via a valid email address with the a domain that matches the sender's domain or is authorized to send on behalf of the domain.

You can try a testing service like MXToolbox to ensure email is being delivered successfully.

  • Yes, I understand there could be many reasons. But the thing is that other plugins that use wp_mail works just fine, and isn't marked as spam. The email adress is existing (a real adress) on the same domain I have setup. Is there any specific headers that must be setup for wp_mail to work?
    – jockebq
    Mar 25, 2019 at 14:43
  • Just tested MXToolbox and all the following shows red crosses: DMARC Compliant (No DNS Found), SPF Alignment, SPF Authenticated, DKIM Alignment, DKIM Authenticated but the email was delivered and it is not blacklisted.
    – jockebq
    Mar 25, 2019 at 14:54
  • 1
    You're using From: Example <[email protected]> in your example. Is this what you have in your code? This should match your sender email. Mar 25, 2019 at 15:00
  • The issue has to do with that I can see now. My mail is sent from [email protected] but when looking at the email source I can see that the sender is actually [email protected] which means it doesn't match. But I cannot figure out how other plugins do this, and work fine with wp_mail, where I can use [email protected] and it will be used in both sender and from.
    – jockebq
    Mar 25, 2019 at 15:17
  • 1
    Sorry, for not being clear. I don't actually use example.com, I replaced that here on stackexchange to hide my URL for the moment. In reality I use my proper URL, and proper server.web-hosting.com
    – jockebq
    Mar 25, 2019 at 15:48

You mentioned in the comments of another answer that the email address used is a real address. However, you didn't mention whether wp_mail() is actually sending through that account (as opposed to from which is different). It may seem confusing, but it's an important distinction.

If you haven't set up to send through that account via SMTP and you're just replacing the "from" address, you are still sending through the web server's email server. There are parts of that process that make your email header look "spammy."

If you're not connecting wp_mail() to SMTP, I highly recommend that you do. That will solve a number of possible spam issues. If you can't or won't, there are still actions you can take.

When not connecting wp_mail() to an authenticated SMTP account, I would recommend that you set the "sender" to be the same value as the "from" address. Otherwise, in the email header, if these are two different values, it raises a red flag for the spam filter. Here's how to do it:

add_action( 'phpmailer_init', 'fix_my_email_return_path' );

function fix_my_email_return_path( $phpmailer ) {
    $phpmailer->Sender = $phpmailer->From;

There's a more detailed overview of the approach in this article.

Your Answer

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

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