0PHP sendmail failing dmarc

I run a VPS which hosts sev­er­al domains. Send­ing mail from these domains has always been tricky. I’ve long used WP SMTP Mail plu­gin for word­press to work around this, but I wanted to get a prop­er fix. After much read­ing I found a prop­er solution

The prob­lem 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 head­ers. This is the extra para­met­ers part and we spe­cify the sender again with a ‑f command

For more info on this there is a good art­icle at Pupun­zi

For wordpress

Simply add the fol­low­ing code to your theme’s functions.php or cre­ate a cus­tom plu­gin 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 Kin­amo for the tip

Leave a Reply