【问题标题】:Send an email to the author when a post is published发布帖子时向作者发送电子邮件
【发布时间】:2020-05-15 11:09:11
【问题描述】:

我想在文章发表后给作者发邮件。

我的代码

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

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

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

感谢您的帮助!

【问题讨论】:

    标签: php wordpress email post author


    【解决方案1】:

    默认情况下,wp_mail 函数发送纯文本电子邮件,说明 post 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 ); 
    }
    

    【讨论】:

    • 谢谢 gael。电子邮件现在发送正常。非常感谢。我投票但我是新来的,这里不显示投票
    • 我不是新来的,但我从来没有问过问题(这里:/)。我猜你可以用绿色的勾号将问题标记为已回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多