0PHP sendmail failing dmarc

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

Found this useful? Please do let us know by dropping a comment below. If you would like to subscribe please use the subscribe link on the menu at the top right. You can also share this with your friends by using the social links below. Cheers.

Leave a Reply