Skip to main content
Login Join
Snippet · PHP

Change the WordPress “From” Email and Name Globally

Shared by Jainil Nagar · June 1, 2026 · @wp_mail_from, wp_mail_from_name, wp_mail_failed

39 views
Back to Snippets
/**
 * Set WordPress outgoing email sender — only if no other plugin already has.
 *
 * Define in wp-config.php:
 *   define( 'WP_MAIL_FROM',      'no-reply@yourdomain.com' );
 *   define( 'WP_MAIL_FROM_NAME', 'Your Site Name' );
 *
 * Will NOT override WooCommerce, FluentMail, WP Mail SMTP, or any plugin
 * that has already customised the from-address or name.
 */


// 1. From Email

add_filter( 'wp_mail_from', 'wpfolks_custom_mail_from', 99 );
function wpfolks_custom_mail_from( string $current_email ): string {

    // Yield to any plugin that already changed the email away from WP default.
    // WP default is wordpress@{host} — anything else means a plugin owns it.
    if ( wpfolks_mail_from_is_customised( $current_email ) ) {
        return $current_email;
    }

    // Use our constant if defined and valid
    if ( defined( 'WP_MAIL_FROM' ) && is_email( WP_MAIL_FROM ) ) {
        return sanitize_email( WP_MAIL_FROM );
    }

    // Multisite-aware fallback: no-reply@<current-site-domain>
    $domain = wpfolks_get_site_domain();
    if ( $domain && is_email( 'no-reply@' . $domain ) ) {
        return 'no-reply@' . $domain;
    }

    return $current_email;
}


// 2. From Name

add_filter( 'wp_mail_from_name', 'wpfolks_custom_mail_from_name', 99 );
function wpfolks_custom_mail_from_name( string $current_name ): string {

    // Yield to any plugin that already changed the name away from WP default.
    // WP core default is literally the string 'WordPress'.
    if ( wpfolks_mail_name_is_customised( $current_name ) ) {
        return $current_name;
    }

    // Use our constant if defined and non-empty
    if ( defined( 'WP_MAIL_FROM_NAME' ) && ! empty( WP_MAIL_FROM_NAME ) ) {
        return wpfolks_sanitize_mail_name( WP_MAIL_FROM_NAME );
    }

    // Fallback to current site blogname
    $blog_name = get_bloginfo( 'name' );
    if ( $blog_name ) {
        return wpfolks_sanitize_mail_name( $blog_name );
    }

    return $current_name;
}


// 3. Detection Helpers

/**
 * Returns true if the email has already been changed away from
 * WordPress core's own default (wordpress@{host}).
 */
function wpfolks_mail_from_is_customised( string $email ): bool {
    if ( ! is_email( $email ) ) return false;

    // WP core builds its default as wordpress@{HTTP_HOST}
    // If the local part is anything other than 'wordpress' it's been customised
    $local_part = strtolower( explode( '@', $email )[0] ?? '' );

    return $local_part !== 'wordpress';
}

/**
 * Returns true if the name has already been changed away from
 * WordPress core's own default (the literal string 'WordPress').
 */
function wpfolks_mail_name_is_customised( string $name ): bool {
    return strtolower( trim( $name ) ) !== 'wordpress';
}


// 4. Shared Helpers

function wpfolks_get_site_domain(): string {
    $url    = network_site_url( '/' );
    $domain = wp_parse_url( $url, PHP_URL_HOST ) ?? '';
    return preg_replace( '/^www./i', '', $domain );
}

function wpfolks_sanitize_mail_name( string $name ): string {
    $name = str_replace( [ "r", "n", "�" ], '', $name );
    $name = trim( $name );
    if ( preg_match( '/[()<>[]:;@,"]/', $name ) ) {
        $name = '"' . addslashes( $name ) . '"';
    }
    return $name;
}


// 5. Debug logging on mail failure

add_action( 'wp_mail_failed', 'wpfolks_mail_from_debug_log' );
function wpfolks_mail_from_debug_log( WP_Error $error ): void {
    if ( ! defined( 'WP_DEBUG_LOG' ) || ! WP_DEBUG_LOG ) return;
    error_log( sprintf(
        '[WPFolks Mail] Delivery failed. From: "%s" <%s>. Error: %s',
        apply_filters( 'wp_mail_from_name', '' ),
        apply_filters( 'wp_mail_from', '' ),
        $error->get_error_message()
    ) );
}
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments