I run a VPS which hosts several domains. Sending mail from these domains has always been tricky. I’ve long used WP SMTP Mail plugin for wordpress to work around this, but I wanted to get a proper fix. After much reading I found a proper solution
The problem is that the “from” address and the “return to” address have to match and unless you know how to fully do the code they wont.
Code for non wordpress sites
$message = 'Your message here'; $sender = 'you@yourdomain.com'; $headers = 'From: ' . $sender . "\r\n" . 'Reply-To: ' . $sender . "\r\n" . 'Return-Path: ' . $sender; $subject = 'Your Subject'; $success = mail('recepient@mail', $subject, $message, $headers, "-f " . $sender);
Note, the key part is the final part, the extra value AFTER the headers. This is the extra parameters part and we specify the sender again with a ‑f command
For more info on this there is a good article at Pupunzi
For wordpress
Simply add the following code to your theme’s functions.php or create a custom plugin with the code
class email_return_path { function __construct() { add_action( 'phpmailer_init', array( $this, 'fix' ) ); } function fix( $phpmailer ) { $phpmailer->Sender = $phpmailer->From; } } new email_return_path();
Thanks to Kinamo for the tip
“Hi James I realise it has been a long while, but I just checked this on windows 11 (build 23H2)…”