Fixing the WordPress Email Return-Path Header

October 27th, 2012

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 Responses to “Fixing the WordPress Email Return-Path Header”

  1. cristian says:

    im not sure where to place this code

    Posted on 16 Apr 2013 Reply
    • Abdussamad says:

      The code is there for other developers to read. All you have to do is the download the plugin and install it like any other WordPress plugin.

      Posted on 17 Apr 2013 Reply
    • cristian says:

      lol, thanks. I was trying to do it the hardest way.

      Posted on 17 Apr 2013 Reply
  2. Cam says:

    Wow thank you very mutch ! Save my life !

    Posted on 27 May 2013 Reply
  3. […] are hosting your mail server and WordPress within your server, be sure you take the first step to ensure that Return-Path is correctly setup and confirm that Return-Path is set correctly by using a plugin to send an e-mail to […]

    Posted on 01 Oct 2013
  4. Tim says:

    Works like a treat, thanks mate!

    Posted on 07 Oct 2013 Reply
  5. Leon says:

    You can also just copy that code and paste it into your child theme’s functions.php. Works for me.

    Posted on 14 Mar 2014 Reply

Leave a Reply

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