Changing the From Name in sent emails

Hello!

I hope you are doing great.

I saw this article and wanted to do the same: https://premium.wpmudev.org/blog/wordpress-email-settings/

I want to set the name of the sender to be the title of the site.

I had this modified code, but it did not work.

function xyz_filter_wp_mail_from_name($from_name){
return wp_title();
}
add_filter("wp_mail_from_name", "xyz_filter_wp_mail_from_name");

How can I fix it?

Thank you!

  • James Morris
    • WordPress Enthusiast

    Hello Carlos,

    I hope you are well today. Thank you for your question.

    Unfortunately, trying to use a template tag like bloginfo('name') in this function will result in problems. You can, however, manually specify the name you want to use and it works well. For example, the following code works perfectly.

    // Function to change email address
    
    function wpb_sender_email( $original_email_address ) {
        return 'wpmudev@example.com';
    }
    
    // Function to change sender name
    function wpb_sender_name( $original_email_from ) {
    	return 'My Test';
    }
    
    // Hooking up our functions to WordPress filters
    add_filter( 'wp_mail_from', 'wpb_sender_email' );
    add_filter( 'wp_mail_from_name', 'wpb_sender_name' );

    I hope that clarifies a bit. Let us know if you have any further questions. We'll be happy to help! :slight_smile:

    Best regards,

    James Morris

      • James Morris
        • WordPress Enthusiast

        Hello Carlos,

        After doing a little tinkering around, this seems to work nicely for single site installs:

        // Function to change sender name
        function wpb_sender_name( $original_email_from ) {
        	$name = get_bloginfo();
        	return $name;
        }
        
        // Hooking up our functions to WordPress filters
        add_filter( 'wp_mail_from_name', 'wpb_sender_name' );

        Let me know if this takes care of it for you.

        Best regards,

        James Morris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.