记录一些常用的函数和瞎折腾的过程……

wordpress回复评论后自动发邮件通知

wordpress在文章评论互动上,若对评论进行了回复,需要发邮件进行通知的这个功能的话,就要自己动下手了,当然,wordpress有着丰富的插件,可以安装插件并实现这个功能,但我是个不愿意用插件的人,我喜欢自己改动代码来实现这个功能。

1、使用SMTP发送邮件通知,在主题的functions.php中加入以下代码:

//SMTP邮箱设置  
function psay_mail_smtp($phpmailer) {  
$phpmailer->From = 'test@test.com'; //发件人地址  
$phpmailer->FromName = 'Program Say'; //发件人昵称  
$phpmailer->Host = 'smtp.qq.com'; //SMTP服务器地址  
$phpmailer->Port = '465'; //SMTP邮件发送端口  
$phpmailer->SMTPSecure = 'ssl';//SMTP加密方式(SSL/TLS),没有为空即可  
$phpmailer->Username = 'test@test.com'; //邮箱帐号  
$phpmailer->Password = 'password'; //邮箱密码  
$phpmailer->IsSMTP();  
$phpmailer->SMTPAuth = true; //启用SMTPAuth服务  
}  
add_action('phpmailer_init', 'psay_mail_smtp'); 

2、发送评论回复通知邮件的详细函数,继续在functions.php中加入:

//评论回复邮件通知  
function comment_mail_notify($comment_id) {  
$admin_notify = '0';  
$admin_email = get_bloginfo ('admin_email');  
$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']));  
$to = trim(get_comment($parent_id)->comment_author_email);  
$subject = '你在 【' . get_option("blogname") . '】上的留言有了新回复';  
$message =  '  
<div style="background-color:#f8f8f8; border:1px solid #D2D1CB; color:#111; -moz-border-radius:8px; -webkit-border-radius:8px; -khtml-border-radius:8px; border-radius:8px; font-size:14px; width:702px; margin:0 auto; margin-top:10px;">  
<div style="background:#0e79cc; width:100%; height:60px; color:white; -moz-border-radius:6px 6px 0 0; -webkit-border-radius:6px 6px 0 0; -khtml-border-radius:6px 6px 0 0; border-radius:6px 6px 0 0; ">  
<span style="height:60px; line-height:60px; margin-left:30px; font-size:20px;"> 您在<a href="' . home_url() . '" style="text-decoration:none; color:#fff;font-weight:600;" target="_blank">'. get_option("blogname").'</a>上的评论有新回复啦!</span>  
</div>  
<div style="width:90%; margin:0 auto">  
<br>  
<p style="font-weight:600;">' . trim(get_comment($parent_id)->comment_author) . ':</p>  
<p>您好,您曾在《' . get_the_title($comment->comment_post_ID) . '》发表了如下的评论:</p>  
<p style="background-color: #fff;border:1px solid #eaeaea;padding: 20px;margin: 15px 0; -moz-border-radius:6px 6px; -webkit-border-radius:6px 6px;; -khtml-border-radius:6px 6px; border-radius:6px 6px;">' . trim(get_comment($parent_id)->comment_content) . '</p>  
<p style="font-weight:600;">' . trim($comment->comment_author) . ':</p>  
<p>已给您做出如下的回复:</p>  
<p style="background-color: #fff;border:1px solid #eaeaea;padding: 20px;margin: 15px 0; -moz-border-radius:6px 6px; -webkit-border-radius:6px 6px;; -khtml-border-radius:6px 6px; border-radius:6px 6px;">' . trim($comment->comment_content) . '</p>  
<p>您可以点击<a href="' . htmlspecialchars(get_permalink($comment->comment_post_ID) . "#comment-" . $comment->comment_ID) . '" style="color:#0e79cc" target="_blank">查看完整内容</a></p><br>  
<p style="font-weight:600;">温馨提示:</p>  
<p>1.本邮件由系统自动发出,请勿直接回复,谢谢!</p>  
</div>  
<div style="background:#0e79cc; width:100%; height:30px; color:white; -moz-border-radius:0 0 6px 6px; -webkit-border-radius:0 0 6px 6px;; -khtml-border-radius:0 0 6px 6px; border-radius:0 0 6px 6px; ">  
<span style="height:30px; line-height:30px; margin-left:30px; font-size:14px;">以后有空请多来访,<a href="' . home_url() . '" style="text-decoration:none; color:#fff;font-weight:600;" target="_blank">' . get_option("blogname") . '</a>在此谢谢您的支持!</span>  
</div>  
</div>  
';  
$message = convert_smilies($message);  
$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);  
}  
};  
add_action('comment_post', 'comment_mail_notify');  

3、加入单选框,让用户选择是否接收邮件通知,这段也加在functions.php中:

// 是否同意接收通知邮件  
function add_checkbox() {  
    echo '<span class="footer-tag"><input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:0px;" /><label for="comment_mail_notify">有人回复时邮件通知我</label></span>';  
}  
add_action('comment_form', 'add_checkbox'); 

若要强制进行邮件通知,不需要用户评论时进行选择是否接收邮件通知,可不用加上述3中的代码,但2中的代码要改为如下的形式:

// 评论邮件通知  
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') && (!get_comment_meta($parent_id,'_deny_email',true)) && (get_option('admin_email') != get_comment($parent_id)->comment_author_email)) {  
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); //可以修改为你自己的邮箱地址  
$to = trim(get_comment($parent_id)->comment_author_email);  
$subject = '你在 [' . get_option("blogname") . '] 的留言有了新回复';  
$message = '<table class="email" style="width:720px;margin:auto;font-size: 16px;line-height: 1.4;font-family:黑体;border: 1px solid #eee;border-radius: 5px;">  
<tbody>  
<tr>  
<td style="padding:5%;color: #666;">  
<div class="email-header">  
<div class="email-logo-wrapper" style="font-size: 30px;padding: 0 0 10px 0;color: #f55;border-bottom: 1px solid #eee;text-align: left;">  
' . get_option("blogname") . '  
</div>  
</div>  
<div>  
<p>' . trim(get_comment($parent_id)->comment_author) . ',您在文章<strong style="font-weight:bold"> 《' . get_the_title($comment->comment_post_ID) . '》 </strong>中的评论:</p>  
<p style="line-height: 36px;padding: 10px;background: #f6f6f6;text-indent: 2em;">' . trim(get_comment($parent_id)->comment_content) . '</p>  
<p>'. $comment->comment_author .' 给您的回复如下:</p>  
<p style="line-height: 36px;padding: 10px;background: #f6f6f6;text-indent: 2em;">' . trim($comment->comment_content) . '</p>  
<a target="_blank" style="color: #fff;background: #f55;width: 200px;padding: 10px 0;border-radius: 5px;margin: 30px 0 0;text-align:center;display:block;" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">立即回复</a>  
</div>  
</td>  
</tr>  
<tr>  
<td style="font-size:12px;text-align:center;color:#b3b3b1">  
<div style="padding:16px;border-top:1px solid #eee">本邮件由 <a target="_blank" style="color:#b3b3b1" href="' . home_url() . '">' . get_option("blogname") . '</a> 后台自动发送,请勿直接回复!</div>  
</td>  
</tr>  
</tbody>  
</table>';  
$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 );  
}  
};  
add_action('comment_post', 'comment_mail_notify');  
《wordpress回复评论后自动发邮件通知》3条评论
  1. 这个很实用,可以免插件!

    山野愚人居 回复
发表评论