post 发表后给作者发邮件

Send an email to the author when a post is published

我想在 post 发布时向作者发送电子邮件。

我有的代码

add_action( 'publish_post', 'send_notification' );
 function send_notification( $post_id ) {    
    $post     = get_post($post_id);
    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    $post_url = get_permalink( $post_id );
    $post_title = get_the_title( $post_id ); 
    $author   = get_userdata($post->post_author);
    $subject  = '$post_title';
    $message .= "<a href='". $post_url. "'>'".$post_title."'</a>\n\n";
    $message .= $content;
    wp_mail($author->user_email, $subject, $message ); 
}

电子邮件正常发送给作者,但他阅读了 post 内容的源代码。

我应该怎么做才能正常通过电子邮件向作者发送 post 而不是来自 post

的代码

感谢您的帮助!

默认情况下,wp_mail 函数发送纯文本电子邮件,说明 post html 标签未转换。

要发送 html 电子邮件,您需要指定 headers:

add_action( 'publish_post', 'send_notification' );
 function send_notification( $post_id ) {    
    $post     = get_post($post_id);
    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);
    $post_url = get_permalink( $post_id );
    $post_title = get_the_title( $post_id ); 
    $author   = get_userdata($post->post_author);
    $subject  = $post_title;
    $message .= "<a href='". $post_url. "'>'".$post_title."'</a>\n\n";
    $message .= $content;
   $headers = array('Content-Type: text/html; charset=UTF-8');
    wp_mail($author->user_email, $subject, $message, $headers ); 
}