Loading
0

纯代码实现WordPress评论回复自动邮件提醒

SMTP邮件功能是WordPress中很常用的功能, 虽然WordPress自带了mail函数,但用自带mail函数发送邮件很容易失败或者被拒收。有很多虚拟主机基本都是禁用了 mail 函数,所以我们只能使用SMTP服务发送邮件,网上关于SMTP的WordPress插件有很多,今天我分享一种纯代码实现WordPress评论回复自动邮件提醒。

纯代码实现WordPress评论回复自动邮件提醒

首先将下面代码修改后复制到functions.php文件,再测试发送邮件功能。

//评论回复邮件

function comment_mail_notify($comment_id) {

$comment = get_comment($comment_id);

$parent_id = $comment->comment_parent ? $comment->comment_parent : '';

$spam_confirmed = $comment->comment_approved;

if (($parent_id != '') && ($spam_confirmed != 'spam')) {

$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));//发件人e-mail地址

$to = trim(get_comment($parent_id)->comment_author_email);

$subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';

$message = '<div><div class="adM">

</div><div><span>您在<a href="' . get_option('home') . '" rel="external nofollow" rel="external nofollow" target="_blank">' . get_option('blogname') . '</a> 上的留言有回复啦!</span> </div>

<div>

<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>

<p>您于' . trim(get_comment($parent_id)->comment_date) . ' 在文章《' . get_the_title($comment->comment_post_ID) . '》上发表评论: </p>

<p>' . nl2br(get_comment($parent_id)->comment_content) . '</p>

<p>' . trim($comment->comment_author) . ' 于' . trim($comment->comment_date) . ' 给您的回复如下: </p>

<p>' . nl2br($comment->comment_content) . '</p>

<p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '" rel="external nofollow" target="_blank">查看回复的完整內容</a></p>

<p>感谢你对 <a href="' . get_option('home') . '" rel="external nofollow" rel="external nofollow" target="_blank">' . get_option('blogname') . '</a> 的关注,如您有任何疑问,欢迎在博客留言,我会一一解答</p><p>(此邮件由系统自动发出,请勿回复。)</p></div></div>';

$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";

$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";

wp_mail( $to, $subject, $message, $headers );

//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing

}

}

add_action('comment_post', 'comment_mail_notify');