How to add Sender header to wp_mail ()

The wp_mail () function in WordPress is used to send an email, but by default, for compatibility with some hosting providers, the kernel developers have disabled the Sender header setting.

The Sender header is very similar to the From header, and in most cases, both headers should have the same meaning. But the key difference between the two is that the From header is part of the message being forwarded, while the Sender header is part of the so-called “envelope”.

This header is most often used for business purposes. For example, if a message cannot be delivered, mail servers will look specifically at the Sender header (as well as the Return-Path) in order to notify the sender of a delivery error. It can also influence the decisions of spam filters installed on certain mail services.

If these headers are not explicitly specified, then many hosting providers generate them automatically based on the hostname where the site is located, and where most likely error reports will not be able to be delivered.

Installing Sender in WordPress

When working with wp_mail () in WordPress, you can add a Sender header using an event phpmailer_initin your own plugin:

add_action ('phpmailer_init', function ($ m) {
$ m-> Sender = $ m-> From;

This method works only in the default configuration, where the sending takes place through the local mail service, which is the most optimal from a performance point of view. If you are using a plugin for a third-party SMTP server, then most likely it will install the required Sender header for you.

Also see: How to Get Webinars on WordPress

If you have access to the mail service responsible for sending e-mails, then you can check the correctness of the configuration in the logs (most often /var/log/mail.log).

It is not always possible to check the correctness on the receiving side, since most mail clients do not hold the “envelope” after successfully receiving a message. But you can rely on the correctness of the address in the Return-Path header, which, like the From header, are part of the letter itself.


Leave a comment

Your email address will not be published.