All emails sent by a clean installation of WordPress go out with the sender name “WordPress” and the address “wordpress@yourdomain.com”.

Since it’s not so nice to receive emails from a site and see the sender name “WordPress” it is usually a good choice to change it. I don’t know why WordPress doesn’t make this option available by default.

Anyway, to change if you need to install a plugin, there is a wide choice, but if you want to just add a few lines of code to your blog, it is pretty easy:

add_filter('wp_mail_from_name', function ($from_name) {
  return "My Name"; 
); 

The variable $from_name contains just “WordPress” and we ignore it.

Where to add such code?

You can add it directly in your functions.php theme file, especially if you have a child theme. But that makes the code theme-dependent which is something I don’t like. Changing the sender name for your emails is not something related to the theme!

The better alternative is to place a file with the code inside the wp-content/mu-plugins folder, which is the folder dedicated to the “must-use” (from which “mu”) plugins, so the snippet will be loaded every time without no need for other configurations.

A similar effect can be obtained by manipulating directly the PHPMailer object used by WP to send emails. Since WP filters on the PHPMailer object are fired after the one we used in this example, they can win the “name-changing battle” and make your code apparently not working.

If all that sounds a bit confusing, don’t mind, it’s worse than you think. Read why WP can fail to send emails (it’s not its fault).

Similar Posts

Leave a Reply