通常在WordPress站点开发中,我们为了增强网站用户的粘度会开启评论系统,但是管理员回复了访客的评论,访客却不能立即知道,这样就降低了网站开启评论系统的效果。今天Gavin就来教大家如何来给空间不支持mai()函数的wordpress站点增加评论回复邮件提醒的方法,为什么说是给空间不支持mai()函数的wordpress站点增加评论回复邮件提醒的方法呢?因为Gavin这次是使用SMTP的方式来发邮件的,下面上代码:
一、让wordpress支持SMTP方式来发送邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//smtp add_action('phpmailer_init', 'mail_smtp'); function mail_smtp($phpmailer) { $phpmailer - >FromName = '云淡风清'; //发信人名称 $phpmailer - >Host = 'smtp.mxhichina.com'; //smtp服务器 $phpmailer - >Port = 25; //端口 $phpmailer - >Username = '*********'; //邮箱帐号 $phpmailer - >Password = '*********'; //邮箱密码 $phpmailer - >From = '*********'; //邮箱帐号 $phpmailer - >SMTPAuth = true; $phpmailer - >SMTPSecure = ''; //tls or ssl (port=25留空,465为ssl) $phpmailer - >IsSMTP(); } |
二、让wordpress评论后回复自动发送回复邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
//评论回复邮件通知(所有回复都邮件通知) 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 = ' <div style="background-color:#fff; border:1px solid #666666; color:#111; -moz-border-radius:8px; -webkit-border-radius:8px; -khtml-border-radius:8px; border-radius:8px; font-size:12px; width:702px; margin:0 auto; margin-top:10px; font-family:微软雅黑, Arial;"> <div style="background:#666666; 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:12px;"> 您在<a style="text-decoration:none; color:#00bbff;font-weight:600;" href="'.get_option('home').'">'.get_option("blogname").' </a>博客上的留言有回复啦!</span></div> <div style="width:90%; margin:0 auto"> <p>'.trim(get_comment($parent_id) - >comment_author).', 您好!</p> <p>您曾在 ['.get_option("blogname").'] 的文章 《'.get_the_title($comment - >comment_post_ID).'》 上发表评论: <p style="background-color: #EEE;border: 1px solid #DDD; padding: 20px;margin: 15px 0;">'.nl2br(get_comment($parent_id) - >comment_content).'</p> <p>'.trim($comment - >comment_author).' 给您的回复如下: <p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px; margin: 15px 0;">'.nl2br($comment - >comment_content).'</p> <p>您可以点击 <a style="text-decoration:none; color:#00bbff" href="'.htmlspecialchars(get_comment_link($parent_id)).'">查看回复的完整內容</a></p> <p>欢迎再次光临 <a style="text-decoration:none; color:#00bbff" href="'.get_option('home').'">'.get_option("blogname").'</a></p> <p>(此邮件由系统自动发出, 请勿回复.)</p> </div> </div>'; $message = convert_smilies($message); $from = "From: \"".htmlspecialchars(get_option('blogname'), ENT_QUOTES)."\" <$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'); |
以上代码加入到functions.php里调用即可。
挺好用的
2017年12月13日 上午11:18能帮到你就是最好的。
2018年1月3日 上午9:47这不是V7V3的吗?
2016年2月8日 上午9:01见多识广啊,共同学习
2016年3月24日 下午5:17好用的经验 是时候抛弃多说了
2016年1月28日 下午2:13很多时候,一个小小的函数,就可以解决一个插件完成的事
2016年3月24日 下午5:16