程序WordPress本身具有邮件通知评论的功能。而我在使用GDD的空间时候安装WP,该功能一直是可用的。所以配置前,还是确认是否需要自己设置。
自动邮件回复游客评论相关插件用得最多的是Mail To Commenter。下面看看代码实现WordPress评论回复自动邮件通知。
登陆博客后台,点击“外观”选项卡下的“编辑”选项进入主题编辑界面;
选项functions.php文件,实现所有回复都发送邮件通知,就在之间添加以下函数:
/* comment_mail_notify v1.0 by willin kan. (所有回复都发邮件) */
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 发出點, no-reply 可改為可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
$message = '
' . trim(get_comment($parent_id)->comment_author) . ', 您好!
您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
'
. trim(get_comment($parent_id)->comment_content) . '
' . trim($comment->comment_author) . ' 給您的回复:
'
. trim($comment->comment_content) . '
您可以点击 查看回复完整內容
欢迎再度光临 ' . get_option('blogname') . '
(此邮件由系统自动发送,请勿回复.)
';
$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, '
' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------
如果想让访客自己选择是否邮件通知,那么就在functions.php文件中的<?php和?>之间添加以下函数,该函数将会在评论框底部生成要不要收回复通知的选项:
/* 开始*/
function comment_mail_notify($comment_id) {
$admin_notify = '1'; // admin 要不要收回覆通知 ( '1'=要 ; '0'=不要 )
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
global $wpdb;
if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
$wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
$spam_confirmed = $comment->comment_approved;
if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
$message = '
' . trim(get_comment($parent_id)->comment_author) . ', 您好!
您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
'
. trim(get_comment($parent_id)->comment_content) . '
' . trim($comment->comment_author) . ' 給您的回應:
'
. trim($comment->comment_content) . '
您可以點擊 查看回應完整內容
歡迎再度光臨 ' . get_option('blogname') . '
(此郵件由系統自動發出, 請勿回覆.)
';
$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, '
' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
/* 自動加勾選欄 */
function add_checkbox() {
echo '';
}
add_action('comment_form', 'add_checkbox');
如果想让博客管理员决定什么情况下发邮件,就在functions.php文件中的<?php和?>之间添加以下函数:
/* comment_mail_notify v1.0 by willin kan. (無勾選欄) */
function comment_mail_notify($comment_id) {
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
/* 上面的判斷式,決定發出郵件的必要條件:
($parent_id != '') && ($spam_confirmed != 'spam'): 回覆的, 而且不是 spam 才可發, 必需!!
($to != $admin_email) : 不發給 admin.
($comment_author_email == $admin_email) : 只有 admin 的回覆才可發.
可視個人需求修改以上條件.
*/
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
$message = '
' . trim(get_comment($parent_id)->comment_author) . ', 您好!
您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
'
. trim(get_comment($parent_id)->comment_content) . '
' . trim($comment->comment_author) . ' 給您的回應:
'
. trim($comment->comment_content) . '
您可以點擊 查看回應完整內容
歡迎再度光臨 ' . get_option('blogname') . '
(此郵件由系統自動發出, 請勿回覆.)
';
$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, '
' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------
引用:博客吧-WordPress博客非插件实现评论回复邮件通知功能
发表回复