admin管理员组

文章数量:1122832

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.

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.

Share Improve this question asked Mar 13, 2018 at 16:32 Kevin RobinsonKevin Robinson 617 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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
    }
}); 

本文标签: hooksFilter wpmail based on content type