# Fix Plain Emails Sent as HTML

> When a plugin switches all emails to HTML, WordPress's own plain emails arrive as one squashed paragraph with broken links; this adds line breaks and clickable links back.

- Type: PHP
- Runs: Run everywhere
- Category: [Email](https://wpide.com/library/category/email/)
- Tags: email, html, fixes
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-fix-plain-emails-as-html/

## Code

```php
add_filter( 'wp_mail', function ( $atts ) {
    $headers = is_array( $atts['headers'] ) ? implode( "\n", $atts['headers'] ) : (string) $atts['headers'];
    $is_html = false !== stripos( $headers, 'text/html' ) || ( false === stripos( $headers, 'content-type' ) && 'text/html' === apply_filters( 'wp_mail_content_type', 'text/plain' ) );
    $message = (string) $atts['message'];
    $probe   = preg_replace( '/<https?:[^>]+>/', '', $message );

    if ( ! $is_html || $probe !== strip_tags( $probe ) ) {
        return $atts;
    }

    $message         = preg_replace( '/<(https?:[^>]+)>/', '$1', $message );
    $atts['message'] = make_clickable( nl2br( esc_html( $message ) ) );

    return $atts;
}, 15 );
```

## Notes

Only touches emails sent as HTML whose body contains no HTML tags, such as the password reset and new user emails.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Fix Plain Emails Sent as HTML" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
