Fixing the WordPress Email Return-Path Header

WordPress LogoEmails sent by WordPress can often end up in the spam folder because of a sub-optimal Return-Path email header. What does that mean and how do you fix it?

The best way to illustrate this problem is with an example. Say you installed WordPress on a server with a Fully Qualified Domain Name (FQDN) of server.example.com. However, the domain name of your WordPress site is foobar.com. In the WordPress admin area under Settings > General you’ve set user@foobar.com as your site’s outgoing email address. The only problem is that the mail server software (MTA), such as Sendmail or Postfix, is adding a return-path header to your WordPress emails that looks something like the following:

Return-Path: <apache@server.example.com>

This does not match the from address of user@foobar.com and that is why some spam filters would flag this email as junk mail. So ideally you want the return-path header to match the from header in emails sent by WordPress.

Fixing The Return-Path Header

WordPress has its own function for sending emails called wp_mail which you can find in wp-includes/pluggable.php. To set the correct return-path header you have to plug into the action “phpmailer_init” that is executed just before an email is sent out:

<?php
/*
Plugin Name: Email Return Path Fix
Author: Abdussamad Abdurrazzaq
*/
class email_return_path {
  	function __construct() {
		add_action( 'phpmailer_init', array( $this, 'fix' ) );    
  	}
 
	function fix( $phpmailer ) {
	  	$phpmailer->Sender = $phpmailer->From;
	}
}
 
new email_return_path();

The above plugin sets the return-path header to equal the from header.

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.

Kudos to Stefano Lissa on the WP forums for writing about this.

7 thoughts on “Fixing the WordPress Email Return-Path Header

Leave a Reply

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