Halloween Sale 15% off Ends in See the plans
PHP

Send Plain WordPress Emails in a Branded HTML Layout

Wraps the plain-text emails WordPress sends (password resets, notifications, form plugins) in a simple HTML layout with your site name and clickable links.

Don’t have WPIDE yet? Install it on your site

Code

add_filter( 'wp_mail', function ( $atts ) {
    $accent = '#2271b1'; // Brand color for the header bar and links.

    $headers = is_array( $atts['headers'] ) ? implode( "\n", $atts['headers'] ) : (string) $atts['headers'];
    $message = (string) $atts['message'];

    if ( stripos( $headers, 'text/html' ) !== false || stripos( $message, '<html' ) !== false || stripos( $message, '<p' ) !== false ) {
        return $atts;
    }

    $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
    $body = make_clickable( nl2br( esc_html( $message ) ) );
    $body = str_replace( '<a href=', '<a style="color:' . esc_attr( $accent ) . ';" href=', $body );

    $atts['message'] = '<!DOCTYPE html><html><body style="margin:0;padding:24px;background:#f0f0f1;font-family:-apple-system,BlinkMacSystemFont,\'Segoe UI\',Roboto,sans-serif;">'
        . '<table role="presentation" width="100%" style="max-width:600px;margin:0 auto;background:#fff;border-radius:6px;overflow:hidden;border-collapse:collapse;">'
        . '<tr><td style="padding:16px 24px;background:' . esc_attr( $accent ) . ';color:#fff;font-size:18px;font-weight:600;">' . esc_html( $site ) . '</td></tr>'
        . '<tr><td style="padding:24px;font-size:15px;line-height:1.6;color:#1e1e1e;">' . $body . '</td></tr>'
        . '<tr><td style="padding:16px 24px;font-size:12px;color:#646970;border-top:1px solid #f0f0f1;">' . esc_html( home_url() ) . '</td></tr>'
        . '</table></body></html>';

    $extra = 'Content-Type: text/html; charset=UTF-8';

    if ( is_array( $atts['headers'] ) ) {
        $atts['headers'][] = $extra;
    } else {
        $atts['headers'] = trim( $headers . "\n" . $extra );
    }

    return $atts;
}, 20 );

Notes

Emails that are already HTML, such as WooCommerce emails, are left untouched. Change $accent to your brand color.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.