Email PHP Intermediate

Send WordPress Emails as Simple HTML

Wrap WordPress's plain-text emails in a clean HTML layout with your site name, readable spacing and clickable links.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 25 lines
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 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • Links wrapped in angle brackets, like <https://…>, are unwrapped first, and the rest of the text is escaped, so nothing in the original email disappears or breaks the layout.
  • Test the result in Gmail, Outlook and Apple Mail. Email apps vary a lot.
  • Runs at priority 20 so it sees changes from other wp_mail filters, such as a Reply-To header.

How to test it

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.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only