Correctly setting return-path for Wordpress mail functionality

Last updated on 06 Sep 2023, 13:16:48.
Category: All about web hosting | Webhosting settings

Apache CentOS Debian PHP Sendmail SMTP Wordpress

Why Wordpress mails sometimes never arrive

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.

The solution: a simple Wordpress plugin

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!


Related articles

Apache - Disable SSL 2.0, SSL 3.0 and opt fore a modern safe SSL config

This article shows you how to disable the SSL 2.0, SSL 3.0 and older TLS version protocols on your Apache...

Read more

Kinamo mail server settings (mail server cheat sheet)

The following article provides the basic settings and server names (domain names) for the Kinamo mail infrastructure. These settings allow...

Read more

Create an automatic SPAM filter in the Kinamo Webmail

This article explains how you may create a filter that will automatically place SPAM messages in a special folder, so...

Read more

Need extra help?

Were not all your questions answered?
Don't worry, we will be happy to help you via a support request!

Select your language

All languages: