0

I created a wp_mail function which I use to send an email when a specific button is pressed. This works great, except that it will always end up in the 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 <noreply@example.com>';

    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 adress used and these will not end up in the spam folder.

Any idea why this happens?

0

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 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 at 14:54
  • You're using From: Example <noreply@example.com> in your example. Is this what you have in your code? This should match your sender email. – MikeNGarrett Mar 25 at 15:00
  • The issue has to do with that I can see now. My mail is sent from noreply@example.com but when looking at the email source I can see that the sender is actually example@server123.web-hosting.com 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 noreply@example.com and it will be used in both sender and from. – jockebq Mar 25 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 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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