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

Managed services in the spotlight

Our system administrators have lifted a couple of cargo ships filled with containers of work. (yes, that pun was on...

Read more

What service level guarantees (SLA) are available for a server housing (co-location)?

Server housing or co-location customers can fall back on an excellent service level guarantee. A Kinamo server housing service comes...

Read more

Discover here how we’ve fixed a blocking error when upgrading ESXi 6.5 to 7.0 update 2 through vSphere Lifecycle Manager.

Kinamo maintains multiple VMware vSphere clusters, not only for our own infrastructure, but also for a variety of customers. As...

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!

Kinamo

Select your language

All languages: