Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

php - wordpress wp_mail with html template renderd content twice

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:

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...