0

Hi i want to set email template for all mails for my site . I know how to set up mail template and i set mail template for each of my mail by editing the code ie

wp_mail( $recipient, $subject,  $body, $headers, $attachments );

is mail code then i replaced it with

ob_start();
             require_once (get_template_directory() . '/mail-templates/contacemail.php');
             $html2 = ob_get_contents(); 
        ob_end_clean();
            return wp_mail( $recipient, $subject,  $html2, $headers, $attachments );

now email template is set up for this mail.[here in contacemail.php echo $body; get the content of body]

Now i need to do for all my emails rather than editing this code for indivdual mails. How to do that? Is there any hook or filter available? please help.

1

Yes, there is a filter for this.

The correct filter is wp_mail and is defined in /wp-includes/pluggable.php Line 135

So the code (maybe in your functions.php) should look something like this:

function mail_template($args){

    ob_start();
    require_once (get_template_directory() . '/mail-templates/contacemail.php');
    $args['message'] = ob_get_contents(); 
    ob_end_clean();

    return $args;

}
add_filter('wp_mail', 'mail_template');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.