levycarneiro
levycarneiro

Reputation: 21

Wordpress hook to modify email body before it gets sent

The scenario: I need to remove some unwanted content from email that gets sent in Wordpress email messages. Basically a preg_replace that needs to happen in the email body before it gets sent to the Wordpress user.

I went through the list of Wordpress hooks and couldn't find a hook for this. Is there another way to achieve this?

Thank you!

Upvotes: 2

Views: 3064

Answers (1)

Adrian Gonzales
Adrian Gonzales

Reputation: 1010

The filter you're looking for is wp_mail

You could perform a preg_replace on the contents like this:

add_filter( 'wp_mail', 'my_replace_mail' );
function my_replace_mail( $args ) {
    if(isset($args['message']) && !empty($args['message'])) {
         $args['message'] = preg_replace("/foo/", "bar", $args['message']);
    }
    return $args;
}

Documentation here: https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail

Upvotes: 2

Related Questions