The wp_mail_content_type Filter Is a Site-Wide Switch, Not a Per-Message One

The failure mode is small enough to miss in a plugin diff and large enough to confuse a publisher for an afternoon: a plugin calls add_filter( 'wp_mail_content_type', ... ), returns 'text/html', sends its own formatted notification, and never removes the filter. Every subsequent wp_mail() call in that request — including core’s password-reset mail — is now handed to PHPMailer as HTML. The reset body was written for text/plain. The recipient sees markup, collapsed line breaks, or a link that no longer parses cleanly.

This is not a spam-filter story. The WordPress documentation describes the consequence as a compatibility problem with password-reset emails and warns of “unexpected problems” with mail from WordPress, plugins, and themes. Whether a malformed message is later classified or rejected downstream is a separate question that the content-type filter does not answer by itself. The useful thing to fix first is the scope of the filter.

Symptom

What the publisher reports is usually one of these:

  • A password-reset email arrives with literal angle brackets, or with all line breaks flattened into one paragraph.
  • A contact-form notification looks correct, but a core transactional email sent in the same request looks wrong.
  • Disabling the plugin that sends formatted mail also fixes the reset email, which makes the plugin look innocent.
  • wp_mail() returns true throughout, so logs show success while the rendered message is wrong.

The last point matters. The wp_mail() reference is explicit that a true return value does not mean the user received the email successfully; it means the method processed the request without errors. A successful handoff to PHPMailer is not evidence that the message was well-formed for its body.

Trace

Read the relevant part of wp_mail() in wp-includes/pluggable.php. The sequence is short and worth knowing by heart:

// If we don't have a Content-Type from the input headers.
if ( ! isset( $content_type ) ) {
    $content_type = 'text/plain';
}

/**
 * Filters the wp_mail() content type.
 *
 * @since 2.3.0
 *
 * @param string $content_type Default wp_mail() content type.
 */
$content_type = apply_filters( 'wp_mail_content_type', $content_type );
$phpmailer->ContentType = $content_type;

// Set whether it's plaintext, depending on $content_type.
if ( 'text/html' === $content_type ) {
    $phpmailer->isHTML( true );
}

Three things follow from that block.

First, the default is text/plain when no Content-Type header is supplied. The filter documentation states this directly: the default content type for wp_mail() is text/plain, which does not allow HTML, and the filter can change it.

Second, the filter receives only the current content-type string. It gets no recipient, subject, caller, or message context. A callback that returns 'text/html' unconditionally therefore changes the content type for every wp_mail() call that runs after it is added in the same request. That is the structural reason a plugin’s local formatting decision leaks into core mail.

Third, when the filtered value equals text/html, core also calls $phpmailer->isHTML( true ). The message is now treated as an HTML body. If the body was written as plain text — as core’s password-reset message is — the mismatch is baked in before PHPMailer ever touches the wire.

The filter is not confined to wp_mail(). wp_staticize_emoji_for_email() also passes the content type through wp_mail_content_type, so a plugin’s global filter affects that code path too. That is a useful confirmation when you are tracing why emoji handling changed at the same time as the reset email.

Root cause

The root cause is a category error: the plugin is using a site-wide filter as if it were a per-message setting.

The filter documentation itself demonstrates the correct shape of a filtered send, and the comment in the example is the whole lesson:

add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
$to = 'sendto@example.com';
$subject = 'The subject';
$body = 'The email body content';
wp_mail( $to, $subject, $body );

// Reset content-type to avoid conflicts -- https://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

The wp_mail() reference repeats the warning in prose: reset wp_mail_content_type back to text/plain after sending, because failing to do so could lead to unexpected problems with emails from WordPress or plugins and themes.

A plugin that adds the filter at load time and never removes it has skipped that step. The filter remains active for the rest of the request, and any later core or plugin mail inherits the HTML content type.

The documentation also names the symptom directly: changing the content type to text/html “will cause problems with password reset emails.” It offers two remedies — adapt the password-reset body for HTML, or reset the content type back to text/plain after the custom send, either explicitly or by removing the callback with remove_filter().

Fix

There are two safe patterns. Pick the one that matches what the plugin actually needs.

Pattern A: pass the header on the one call

If the plugin needs HTML for its own message only, do not touch the filter. Pass the Content-Type header in the $headers argument of that specific wp_mail() call. The filter documentation shows this as an alternative to the filter:

$to      = 'sendto@example.com';
$subject = 'The subject';
$body    = '<p>The email body content</p>';
$headers = array( 'Content-Type: text/html; charset=UTF-8' );

wp_mail( $to, $subject, $body, $headers );

This is the per-message tool. It affects one message and leaves every other wp_mail() call in the request at its default.

Pattern B: scope the filter to the send

If the plugin must use the filter — for example, because it is reusing an existing code path that expects the filter — add it immediately before the send and remove it immediately after:

function myplugin_set_html_content_type() {
    return 'text/html';
}

add_filter( 'wp_mail_content_type', 'myplugin_set_html_content_type' );
wp_mail( $to, $subject, $body );
remove_filter( 'wp_mail_content_type', 'myplugin_set_html_content_type' );

The filter documentation’s own example uses this shape, and the wp_mail() reference endorses the reset. The failure mode is the version without the remove_filter().

What not to do

Do not add the filter at plugin load and rely on the site “mostly sending HTML anyway.” The filter has no message context, so “mostly” is not a property it can express. If the site as a whole wants HTML mail, that is a deliberate site-wide decision, and the password-reset body must be adapted for HTML as the documentation describes. That is a different project from a plugin formatting its own notification.

Verification

For a self-maintained install, the first move is a code search, not a plugin toggle. Find every callback on the filter and check whether each one is removed:

wp plugin list --status=active --field=name | while read slug; do
  grep -RIn "wp_mail_content_type" "wp-content/plugins/$slug" 2>/dev/null \
    | sed "s|^|$slug: |"
done

Read the hits. A callback added inside a function that also calls wp_mail() and then remove_filter() is the safe shape. A callback added at file scope or on plugins_loaded with no matching remove_filter() is a candidate for the symptom.

To confirm the effect on a specific message, hook phpmailer_init and inspect the object before it is sent. The hook fires after PHPMailer is initialized and receives the instance by reference:

add_action( 'phpmailer_init', function ( $phpmailer ) {
    error_log( sprintf(
        'phpmailer_init: ContentType=%s isHTML=%s Subject=%s',
        $phpmailer->ContentType,
        $phpmailer->ContentType === 'text/html' ? 'true' : 'false',
        $phpmailer->Subject
    ) );
} );

Trigger a password reset and read the log line. If ContentType is text/html for a reset message, the filter is leaking. If it is text/plain for the reset and text/html only for the plugin’s own message, the scoping is correct.

Finally, do not treat a true return from wp_mail() as verification. The reference is clear that the return value only means the request was processed without errors. Verify the rendered message — the reset email in a real inbox, or the captured message in your mail log — not the boolean.

FAQ

Does the filter cause emails to be marked as spam?

The retrieved WordPress documentation does not make that claim. It documents a compatibility problem with password-reset emails and warns of unexpected problems with mail from WordPress, plugins, and themes. Spam classification and rejection are downstream decisions made by receiving systems, and the content-type filter does not by itself establish why a particular message was filtered. Treat the malformed body as the thing you can prove and fix; treat deliverability as a separate investigation.

Why does the plugin’s own email look fine while the reset email does not?

Because the plugin’s body was written for HTML and the reset body was written for plain text. The filter changes the declared content type for both; only one body matches it. That asymmetry is the tell that the filter is global and the bodies are not.

Can I just set the filter to text/html site-wide and adapt the reset email?

Yes, and the documentation describes that remedy: modify the password-reset body so it works with text/html. That is a site-wide decision with site-wide consequences, and it should be made deliberately rather than as a side effect of a plugin’s notification code.

Where should a plugin add headers instead?

For per-message headers, use the $headers argument of wp_mail(). For changes that need the PHPMailer object itself — SMTP configuration, custom headers, multipart bodies — use the phpmailer_init action, which fires after PHPMailer is initialized and passes the instance by reference.

What about emoji in emails?

wp_staticize_emoji_for_email() also passes the content type through wp_mail_content_type. A global filter affects that path too, which is one more reason to scope the filter to the send that needs it.

Sources

  • WordPress Developer Reference, wp_mail_content_type filter — default content type, the password-reset warning, the remove_filter() example, and the $headers alternative.
  • WordPress Developer Reference, wp_mail() — source of the content-type block, the reset warning, and the note that a true return does not mean delivery.
  • WordPress Developer Reference, phpmailer_init — the action for working with the PHPMailer instance directly.
  • WordPress Developer Reference, wp_staticize_emoji_for_email() — the second code path that runs the same filter.