How to avoid WordPress emails ending up in your spam folder

Posted on

Estimated reading time: 2 minutes, 11 seconds

Wp Email Fix

Often emails sent by WordPress can end up in your spam folder, because it isn’t setting the message envelope properly. So the actual message envelope is using the server hostname which is just not correct, it will work but it is not ideal.

To fix this issue, you should setup WordPress to send emails properly and set the full message envelope correctly.

What does this mean?

The best way to explain this problem is with an example. Let’s say you have your WordPress website (let’s call it, mysite1.com) on a server with Fully Qualified Domain Name (FQDN) of host.uk.myserver.com.  By default when WordPress sends out emails (for example password reminder, new user signup, etc..) it sets your email address from admin area under Settings > General to be the from address header like “From: WordPress <wordpress@mysite1.com>”. The problem is that WordPress do not specify a “Return-Path Header” therefore the mail server software (MTA) such as Sendmail or Postfix, uses your server’s host name as Return-Path instead, which looks something like the following:

Return-Path: <mysite1@host.uk.myserver.com>

This return-path does not match neither the from address ‘wordpress@mysite1.com’ or doesn’t contains your domain name ‘mysite1.com’ that is why some spam filters in the email clients would filter this email as junk mail. So to resolve this we need to make sure the return-path header matches the from address header in all emails sent out by WordPress website.

How do you fix The Return-Path Header?

WordPress uses it’s own function called wp_mail for sending emails, this function has an action ‘phpmailier_init’ which executes just before an email is sent. We can use this ‘phpmailer_init’ action via a plugin to set a custom return-path which matches our from address and/or domain name.

class custom_return_path {
 function __construct() {
 add_action( 'phpmailer_init', array( $this, 'wpemailfix' ) ); 
 }

function wpemailfix( $phpmailer ) {
 $phpmailer->Sender = $phpmailer->From;
 }
}
new custom_return_path();

Above code replaces default return-path value with from address.

WordPress Email Fix Plugin by Webdezign

You can download the plugin which does this fix without needing to do any configurations. Download ‘Wp Email Fix’

If you are using sendmail on your server you have to take an additional step and add the web server username (usually apache) to your /etc/mail/trusted-users file and restart sendmail. If you don’t do this sendmail will add an X-Authentication-Warning header which is likely to cause your email to end up in the spam folder. You don’t have to take this step if you are using Postfix.

Useful Guides

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top