0

TIL that, by default, that Wordpress does not use the Administrator Email address to send password reset emails.

Is there a known/accepted reason why Wordpress behaves this way? (It does not seem intuitive or logical to me - but I may be missing something obvious)

1
  • 2
    Because the administrator is not sending the emails, WordPress is. Oct 18, 2022 at 6:42

1 Answer 1

1

Is there a known/accepted reason why Wordpress behaves this way? (It does not seem intuitive or logical to me - but I may be missing something obvious)

Yes; because that's how WP's wp_mail() function works.

To explain that fully, in wp_mail(), if there is no value for $from_email (which there will not be in a plain vanilla install) it runs the following:

if ( ! isset( $from_email ) ) {
    // Get the site domain and get rid of www.
    $sitename   = wp_parse_url( network_home_url(), PHP_URL_HOST );
    $from_email = 'wordpress@';

    if ( null !== $sitename ) {
        if ( 'www.' === substr( $sitename, 0, 4 ) ) {
            $sitename = substr( $sitename, 4 );
        }

        $from_email .= $sitename;
    }
}

If you're able to grok that code, you should see the answer to your question of "why"? If not, the explanation is that if $from_email is not set, you'll get $from_email with a value of wordpress@your_domain.com.

So that's why.

So, how do you get it to be your admin email?

The following filter dropped wherever you store your custom code snippets (child theme functions.php, custom plugin file, code snippet plugin, etc) will do it:

add_filter( 'wp_mail_from', function( $from_email ) {
    return "my_from_address@my_domain.com";
});

That's a simple and quick method and will get you exactly what you asked for. But, I can't leave it at that. What if you had plugins that may use a different "from" address (thus needing something separate from your filter above)? Essentially, if you want a full featured option that doesn't "step on toes", then you want to make sure you're only filtering the result when it's the pesky defualt ("wordpress@your_domain.com"). Here's how you can do that so it only changes it if it's the default value:

add_filter( 'wp_mail_from', function( $from_email ) {

    // Repeats the action of getting the default address for "Just in Case" checking in the next step
    $sitename = wp_parse_url( network_home_url(), PHP_URL_HOST );
    $check_email = 'wordpress@';
    if ( null !== $sitename ) {
        if ( 'www.' === substr( $sitename, 0, 4 ) ) {
            $sitename = substr( $sitename, 4 );
        }

        $check_email .= $sitename;
    }

    // First make sure it's just default (in case you have a plugin that may use wp_mail() with a specific "from" address):
    if ( $check_email == $from_email ) {
        
        // Get the admin email option value.
        $admin_email = get_option( 'admin_email' );

        // Make sure it's a valid email (for error checking in case someone screwed up something by messing with the db)
        if ( is_email( $admin_email ) ) {
            // If it's valid, use it.
            $from_email = $admin_email;
        }
    }
    return $from_email;
});

Both options use the wp_mail_from filter, documented here. You can also add a filter for wp_mail_from_name if necessary. Same idea, but returning the "name" to go with the email address.

Other methods exist. These are just 2 possibilities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.