Filter wp_mail based on content type

I can current filter the content type of the wp_mail function:

add_filter( 'wp_mail_content_type', function( $content_type ) {
    return 'text/html';
});

And I can filter the message content

add_filter( 'wp_mail', function( $args ) {
    $args['message'] = 'Filtered text here';
    return $args;
});

But how can I only do the latter conditionally?

I only want to filter the message when the content type == text/plain.

I'm guessing there is a super-simple solution to this, but I haven't worked it out yet.

Topic wp-mail email filters hooks Wordpress

Category Web


Note that the wp_mail filter runs before the wp_mail_content_type filter.

If you're using the PHP Mailer then you can try (untested):

add_action( 'phpmailer_init', function( $phpmailer ) {
    if( 'text/plain' === $phpmailer->ContentType ) {
        $phpmailer->Body = 'Filtered text here'; // <-- Message override
    }
}); 

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.