WordPress Mail Routed via Postfix SMTP Relays

By default, the WordPress wp_mail function will not set the actual sender for sendmail and mail backends. This means that the Postfix relay will set the sender to the default $domain instead of the actual sender. Which, in turn, means that when using relay maps (sender_dependent_relayhost_maps) to map senders to correct SMTP relays none of them is matched because of the sender is not set correctly…


Fortunately, the mailer class used by WordPress, PHPMailer knows how to correctly specify the sender via the chosen backend (which in WordPress is the mail backend). By setting the Sender property of the mailer we’re able to have Postfix identify the sender correctly.

The wp_mail function is both pluggable and allows the mailer to be configured before sending by setting the Sender parameter to its From parameter.

add_action( 'phpmailer_init', function( &$phpmailer ) {
    $phpmailer->Sender = $phpmailer->From;
} );

This will allow Postfix to correctly map the sender and apply all the relaying that is necessary.

Why isn’t this being set in wp_mail to begin with? Simply because it “can be changed”: ticket #6861, even if it appears to be a non-sane default behavior resulting in undelivered mail and mail being caught by spam filters. Of course this is being worked on in ticket #18792. A plausible explanation by Otto lies here, which states that WordPress depends on a potentially non-existent e-mail account wordpress@.... This is true, but this will result in a mismatch between the actual sender and the mail headers, which can result in spam filtering and non-delivery.

So, overall, wp_mail is broken by default and needs specific configuration when setting WordPress up.