# Add a Plain-Text Version to HTML Emails

> Generates a plain-text alternative for HTML emails that do not include one, which spam filters like and text-only email clients need.

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

## Code

```php
add_action( 'phpmailer_init', function ( $phpmailer ) {
    if ( 'text/html' !== $phpmailer->ContentType || '' !== $phpmailer->AltBody ) {
        return;
    }

    $text = preg_replace( '#<a[^>]+href=["\']([^"\']+)["\'][^>]*>(.*?)</a>#is', '$2 ($1)', $phpmailer->Body );
    $text = preg_replace( '#<(br|/p|/div|/h[1-6]|/tr|/li)[^>]*>#i', "\n", $text );
    $text = wp_strip_all_tags( $text );
    $text = html_entity_decode( $text, ENT_QUOTES, 'UTF-8' );
    $text = preg_replace( "/\n{3,}/", "\n\n", $text );

    $phpmailer->AltBody = trim( $text );
} );
```

## Notes

Links are kept as "text (url)" in the plain-text version.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Add a Plain-Text Version to HTML Emails" 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/
