Wrap WordPress's plain-text emails in a clean HTML layout with your site name, readable spacing and clickable links.
add_filter( 'wp_mail', function ( array $args ): array {
$headers = is_array( $args['headers'] ) ? implode( "\n", $args['headers'] ) : (string) $args['headers'];
// Leave emails that are already HTML (WooCommerce, forms, newsletters) alone.
if ( stripos( $headers, 'text/html' ) !== false || str_contains( (string) $args['message'], '<html' ) ) {
return $args;
}
$site = esc_html( wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
// Drop the <angle brackets> WordPress puts around some links, then escape the text.
$text = preg_replace( '#<(https?://[^\s>]+)>#', '$1', (string) $args['message'] );
$body = make_clickable( wpautop( esc_html( $text ) ) );
$args['message'] = '<!DOCTYPE html><html><body style="margin:0;padding:24px;background:#f4f4f7;font-family:-apple-system,Segoe UI,Roboto,Arial,sans-serif;color:#1f2937">'
. '<div style="max-width:560px;margin:0 auto;background:#fff;border-radius:8px;padding:28px;line-height:1.6;font-size:15px">'
. '<p style="margin:0 0 20px;font-weight:700;font-size:18px">' . $site . '</p>'
. $body
. '</div></body></html>';
$extra = is_array( $args['headers'] ) ? $args['headers'] : array_filter( explode( "\n", str_replace( "\r\n", "\n", $headers ) ) );
$extra[] = 'Content-Type: text/html; charset=UTF-8';
$args['headers'] = $extra;
return $args;
}, 20 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WordPress sends its own emails, like password resets and new user notices, as plain text. They work, but look bare next to the rest of your brand. This snippet wraps them in a simple, mobile-friendly layout.
The wp_mail filter runs for every email. If the email is already HTML, the snippet does nothing, so WooCommerce, form plugins and newsletters keep their own designs.
For plain-text emails, it escapes the text with esc_html(), turns line breaks into paragraphs with wpautop() and links into clickable links with make_clickable(), then wraps it in a card layout with inline styles that email apps understand. A Content-Type: text/html header tells email apps to show it as HTML.
<https://…>, are unwrapped first, and the rest of the text is escaped, so nothing in the original email disappears or breaks the layout.wp_mail filters, such as a Reply-To header.Use “Lost your password?” on a test account. The email should arrive with your site name at the top in a white card, and the reset link should be clickable.