Emails 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.
im not sure where to place this code
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.
lol, thanks. I was trying to do it the hardest way.
Wow thank you very mutch ! Save my life !
Works like a treat, thanks mate!
You can also just copy that code and paste it into your child theme’s functions.php. Works for me.
This isn’t working for me 🙁 I installed the plugin and still my emails get sent to spam.
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.
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!
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
Thank you very much.
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.
Is this still a problem in the latest WP and is this plug-in still relevant?
Thank you very much! two weeks after, finally I’ve found the solution!
Thank you very much for sharing the plugin, with its use I finished a problem that took little more than a week
Regards
You are a genius, thx!!!
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.
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?
I suggest using a transactional email gateway like sendgrid if you want a higher chance of hitting inboxes.
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/.
Thanks – worked perfectly for me.
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?
Great Thank you!!
Thank you very much. This snippet solved my problem.
YOURE THE BEST .THANK YOU SO SO MUCH
Worked perfect! thanks
Sorry, does not work für woocommerce orders.
best regards,Christian
Doesn’t work – wordpress 4.9.3.
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!!
Works! thank you!
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.
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.
Still works. Thanks a lot!
Still works in 2024. I’m truly grateful.