admin管理员组

文章数量:1134247

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>
    <body>
      Testing
    </body>
    </html>";
    $headers[] = 'From: Example <[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_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?

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>
    <body>
      Testing
    </body>
    </html>";
    $headers[] = 'From: Example <[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_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?

Share Improve this question edited Dec 14, 2019 at 5:17 butlerblog 5,0813 gold badges26 silver badges42 bronze badges asked Mar 25, 2019 at 14:23 jockebqjockebq 4631 gold badge6 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

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.

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.

本文标签: phpEmail always ends up in spam