Tags for this FAQ item:
Apache CentOS Debian PHP Sendmail SMTP Wordpress

To what extent has this article answered your question?

Rated 2 stars, based on 8 votes

Correctly setting return-path for Wordpress mail functionality

Last updated: 31/03/2017

The problem with mails sent through Wordpress - whether it is by using Contact Form 7 plugin or others - is the way the wp-mail function (which most of these plugins use) sends e-mail.

Wordpress uses the PHP mail function. Sending mail with the PHP mail function is no problem of course, but often the return-path header of the message isincorrect. This makes messages end oup in spam and junk folders or worse, they never arrive. All very exciting, but how can you fix it?

The return-path can be set up at server level, through the php.ini file or with an additional parameter setting the From: header which is passed to the PHP mail function.

The best way to explain this, is with an example: the mail is sent from Wordpress on domain name www.mywordpressblog.be, but the site itself runs on a cluster of webservers, so for the server, the sender is not www.mywordpressblog.be but one of the web nodes, for example. web-php55-l01.hosting.kinamo.be.
Through the Wordpress settings you have set up the sender with your e-mail address (webmaster@mywordpressblog.be) but this is not enough: for the mail server (MTA), for example postfix, the return-path header is added if not available, so this becomes:

Return-Path: <apache@web-php55-l01.hosting.kinamo.be>

This is not correct, since it does not match your @mywordpressblog.be address.

There are a dozen ways to fix this: by hacking Wordpress, adding functions, tweaking settings. The easiest way we found was by using a simple plugin, created by Abdussamad.

This code sets the return-path header so it matches the From: header and it will be accepted by the mail server and makes sure it does not end up in spam, junk or oblivion.

Copy the code below and put it in a new file, named email_return_path.php and add it to the wp-content/plugins/ folder.
Don't forget to log in into Wordpress admin and activate the plugin!

<?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();
?>

Have fun!