Trying to add custom html template to wordpress' wp_mail function with:
public function doEmails() {
add_filter( 'wp_mail', [$this,'custom_mails'] );
}
public function custom_mails( $atts ) {
$body = file_get_contents(__DIR__ . '/assets/mail.default.html');
$atts['message']= str_replace('%CONTENTS%', $atts['message'],$body);
$headers = array('Content-Type: text/html; charset=UTF-8');
$atts['headers'] = $headers;
return $atts;
}
Seems pretty easy and works - replaces %CONTENTS% in template file and sends the mail. But also seems to put in email content twice - one in the correct place, and then wraps it again in the content. So I get this:
The highlited part is the mail content.
I also tried version with
ob_start();
include( __DIR__ . '/assets/mail.standard.html');
$email_body = $atts['message'];
$email_content = ob_get_contents();
$email_content = str_replace('%CONTENT%',$email_body,$email_content);
ob_end_clean();
Which is used by other soft uch as Woocommerce, to the same effect - the content gets rendered twice. Has anyone had this kind of problems and may know a solution? Thank you in advanced for any suggestions.
question from:
https://stackoverflow.com/questions/65660977/wordpress-wp-mail-with-html-template-renderd-content-twice 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…