Customize WordPress email headers

On many systems using the default WordPress configuration, emails originating from a WordPress site will likely have some undesirable traits. These are:

From: WordPress wordpress@domain.com
Return-Path: apache@mail.domain.com

It’s likely you’ve seen this before. It’s pretty much the default. The problem is, it’s useless most of the time.

Let’s fix that.

Change the values in the function below and add this code to your functions.php, or make it a plugin and activate it.

<?php
/*
Plugin Name: John's Rewrite phpmailer Sender
Plugin URI: https://www.johnparris.com/
Description: Rewrites mail_from (From), from_name (FromName), and Return-Path (Sender).
Version: 0.10
Author: John Parris
Author URI: https://www.johnparris.com/
License: GPLv2
*/

function wp_mail_to_smtp( $phpmailer ) {
    $phpmailer->Sender   = 'return-path@domain.com';
    $phpmailer->From     = 'mail-from@domain.com';
    $phpmailer->FromName = 'From Name';
}
add_action('phpmailer_init', 'wp_mail_to_smtp');