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.

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

    • There can be many reasons for this. Some things you can try are setting an SPF record in your DNS settings for your domain name and implementing DKIM. It would probably be easier to just use a specialist SMTP service like mailgun, mandrill, amazon SES etc. These days it is very hard for a new IP address to build up a good reputation and get accepted as legitimate by the major free email providers. That is why these transactional email providers have made space for themselves.

  1. Can’t install & getting this error message :
    The package could not be installed. PCLZIP_ERR_BAD_FORMAT (-10) : Unable to find End of Central Dir Record signature

    • Ok, I got it : I was trying to upload, but a simple dir copy did it.
      I’ll now try to reproduce these emails before rejoice too quickly… 😉
      Thanks!

  2. Thank you for this. I was trying to override the Return-Path header through the wp_mail request without success. Your code fixed the issue for me.

    Andy

  3. Hi,

    I just installed your plugin, but still got the same problem, the return path is the bouce+… thing that keep email rejected

    Thank you

    • Then you should talk to your hosting provider about this because they are likely changing it at the server level.

  4. Thank you very much for sharing the plugin, with its use I finished a problem that took little more than a week

    Regards

  5. Really This is great works for me! I have tried many way to solve spam issues but I couldn’t then I got that plugin and its solve my issue. Thank you very much for this nice plugin.

  6. Thank you for this plugin. It is working fine for my booking plugin and contactforms, however with woocommerce it still gets marked as spam.. Any solutions?

  7. Hello

    i have error massage: PCLZIP_ERR_BAD_FORMAT (-10) : Unable to find End of Central Dir Record signature

    What the problem?

    • if you using the plugin installation option in WP admin then your supposed to upload the zip file as is. alternatively you can unzip the file and upload the folder to wp-content/plugins/.

  8. This still works with the latest WordPress release and Woocommerce confirmation emails! Thanks a lot! I can’t believe this isn’t already part of the standard WordPress releases. What’s the point of confirmation emails/emails in general if they never make it to your users/customers?

  9. Thank you so much.

    I was trying the configuration with autentication.
    But your plugin solve and now I can receive my e-mails website.

    Thanks a lot!!

  10. I’m using this with Formidable forms, however it doesn’t work as expected.
    In my form, the “From” field is set as: Test User
    This plugin changes the “Return-Path” to:
    Return-Path: (it looks like a mix of the username and domain)
    It looks as though the regex is failing somewhere?
    I presume it’s not accounting for having the user name filed as well as the address?

    Thanks.

  11. Its outrageous that WordPress still hasen’t fixed this issue in their own codebase and that users still need to rely on installing this as a plugin.

Leave a Reply to Fernando del Peral Cancel reply

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