发表文章后向作者发送电子邮件

时间:2020-05-15 11:09:11

标签: php wordpress email post author

我想在帖子发布后向作者发送电子邮件。

我拥有的密码

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 ); 
}

电子邮件通常会发送给作者,但他会阅读帖子内容的源代码。

我应该怎么做才能正常通过电子邮件而不是帖子中的代码向作者发送帖子

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

要发送HTML电子邮件,您需要指定标题:

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 ); 
}