wordpress評論回復(fù)郵件功能增加讀者回訪,對網(wǎng)站pv數(shù)量有貢獻(xiàn),對于用戶體驗也是非常好的。想象一下,如果你在朱海濤自媒體博客上做過評論,然后有其他讀者回復(fù)你的評論,這是你就會收到一封郵件提醒你的評論被回復(fù)了。這個功能是不是非常貼心,對于網(wǎng)站經(jīng)營者而言,提供這個功能也是能極好的再次吸引讀者的。我的wordpress主題自帶評論回復(fù)發(fā)送郵件提醒的功能,所以我一直沒有去測試這個功能。只到有一天,偉偉提醒我,我的博客回復(fù)評論功能沒有開通,我才開始重視這件事情。
評論回復(fù)郵件效果如如下
我先來說說實現(xiàn)這一個功能的常規(guī)套路,這個方式需要確認(rèn)你的主機支持mail函數(shù)。否則的話,是不起作用的。下面直接上代碼
- //modify by stcash.com
- function?comment_mail_notify($comment_id) {
- ?$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 : '';
- ?$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)) {
- ?$wp_email?= 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
- ?$subject?= '您在 [' . get_option("blogname") . '] 的留言有了新回復(fù)';
- ?$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;">
- ?<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:non; color:#ff0;font-weight:600;"> [' . get_option("blogname") . '] </a>上的留言有回復(fù)啦!</span></div>
- ?<div style="width:90%; margin:0 auto">
- ?<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
- ?<p>您在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />
- ?<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">'. trim(get_comment($parent_id)->comment_content) . '</p>
- ?<p>' . trim($comment->comment_author) . ' 給你的回復(fù):<br />
- ?<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">'. trim($comment->comment_content) . '</p>
- ?<p>你可以點擊<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看完整
- 內(nèi)容</a></p>
- ?<p>歡迎再度光臨<a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
- ?<p>(此郵件由系統(tǒng)自動發(fā)出, 請勿回復(fù)。)</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?);
- ?}
- }
- add_action('comment_post', 'comment_mail_notify');
值得注意的是,朱海濤剛開始增加這個函數(shù)的時候,一直報錯,導(dǎo)致網(wǎng)站返回500錯誤而無法訪問。原來是我的主題中有個文件中已經(jīng)包含了這個函數(shù)。所以最好是在使用這個函數(shù)之前,使用if (!function_exists('comment_mail_notify')) 來判斷下是否本來已經(jīng)存在這個函數(shù)。
然而我的主機好像不支持上面的代碼,應(yīng)該是主機對wp_mail函數(shù)不支持。所以我換了另外一種方式來實現(xiàn),主要原理是使用smtp接口來實現(xiàn)
- //使用smtp發(fā)送郵件(請根據(jù)自己使用的郵箱設(shè)置SMTP)
- add_action('phpmailer_init', 'mail_smtp_2');
- function?mail_smtp_2( $phpmailer?) {
- ??? $phpmailer->FromName = '朱海濤自媒體'; //發(fā)件人名稱
- ??? $phpmailer->Host = 'smtp.qq.com'; //修改為你使用的郵箱SMTP服務(wù)器
- ??? $phpmailer->Port = 465; //SMTP端口
- ??? $phpmailer->Username = 'stcash@stca
sh.com'; //郵箱賬戶 - ??? $phpmailer->Password = 'xz2015'; //郵箱密碼
- ??? $phpmailer->From = 'stcash@stca
sh.com'; //郵箱賬戶 - ??? $phpmailer->SMTPAuth = true;
- ??? $phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25時->留空,465時->ssl)
- ??? $phpmailer->IsSMTP();
- }
這段代碼在主機上是通用的。只需要修改發(fā)件人名稱,郵箱SMTP服務(wù)器?和郵箱賬戶
附上各郵箱的端口及smtp地址:
|
188 郵箱
|
pop3.188.com | smtp.188.com | 端口:25 |
|
163 郵箱
|
pop3.163.com | smtp.163.com | 端口:25 |
|
126 郵箱
|
pop3.126.com | smtp.126.com | 端口:25 |
|
netease 郵箱
|
pop.netease.com | smtp.netease.com | 端口:25 |
|
yeah 郵箱
|
pop.yeah.net | smtp.yeah.net | 端口:25 |
|
QQ 郵箱
|
pop.qq.com | smtp.qq.com | 端口:465或587 |
嫌麻煩的站長可以直接使用后面一種方式來實現(xiàn)評論回復(fù)郵件提醒了。
來源:朱海濤自媒體(微信/QQ號:81433982),原創(chuàng)文章如轉(zhuǎn)載,請注明本文鏈接: http://www.stcash.com/6514

美圖在這里:http://www.fydzv.com/
不錯的貼,太喜歡了,大家都頂啊