WordPress實現(xiàn)回復(fù)文章評論后發(fā)送郵件通知的功能
發(fā)布時間:2016-10-11 11:38:02 作者:佚名
我要評論

這篇文章主要介紹了WordPress實現(xiàn)回復(fù)文章評論后發(fā)送郵件通知的功能,涉及wordpress針對評論與郵件的相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了WordPress實現(xiàn)回復(fù)文章評論后發(fā)送郵件通知的功能。分享給大家供大家參考,具體如下:
很多時候,人們都希望在自己的評論被管理員回復(fù)后會收到通知。該函數(shù)的作用就是回復(fù)后自動郵件通知評論者。
把下面的代碼加到wordpress的主題函數(shù)里面,然后修改下郵件帳號密碼。
該函數(shù)是針對SAE平臺的wordpress,非SAE平臺不能使用,有需要的話留言我也會寫出相應(yīng)方法。
復(fù)制代碼
代碼如下://郵件回復(fù)
function comment_mail_notify($comment_id) {
define('MAIL_SMTP', 'smtp.exmail.qq.com'); //smtp服務(wù)器
define('MAIL_PORT', 25); //smtp端口
define('MAIL_SENDEMAIL', '123456789@qq.com'); //發(fā)送郵件帳號
define('MAIL_PASSWORD', '123456'); //發(fā)送郵件密碼
$admin_notify = '1';
$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 ? '1' : '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") . '回復(fù)被關(guān)注啦~';
$message = '
<div style="width: 502px; height: auto; margin-bottom: 50px; margin-left: auto; margin-right: auto; font-size: 13px; line-height: 14px;">
<div style="width: 502px; margin-top: 10px;">
<div style="font-size: 16px; color: #373737; text-align: center;">'.get_bloginfo("name").'</div>
<div style="font-size: 15px; color: #f0f7eb; padding: 9px; margin-top: 20px; overflow: hidden; background: #299982; padding-left: 30px; padding-right: 40px;">你在 '. get_the_title($comment->comment_post_ID) .' 的評論有了回復(fù):</div>
</div>
<div style="width: 420px; margin-top: 30px; padding: 0 40px 20px; border-left: 1px dashed #299982; border-right: 1px dashed #299982; color: rgba(0,0,0,0.7); background: #f9f9f9; overflow: hidden;">
<div class="one origin" style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0;"><span style="color: #299982;">'. trim(get_comment($parent_id)->comment_author) .'</span>:'. trim(get_comment($parent_id)->comment_content) .'</div>
<div class="one reply" style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0 1em 60px;"><span style="color: #299982;">'. trim($comment->comment_author) .'</span>:'. trim($comment->comment_content) .'</div>
<p style="margin-bottom: 10px;">點擊<a href="' . htmlspecialchars(get_comment_link($parent_id)) . ' style=">查看完整內(nèi)容</a></p>
<p style="margin-bottom: 10px;">(此郵件由系統(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";
$mail = new SaeMail(); //對象
$mail->setOpt(array( 'from' => 'admin@xtwind.com', 'to' => trim($to),//接收信箱
'smtp_host' => MAIL_SMTP , //host
'smtp_port' => MAIL_PORT, //port
'smtp_username' => MAIL_SENDEMAIL,
'smtp_password' => MAIL_PASSWORD,
'subject' => $subject,
'content' => $message,
'content_type' => 'HTML'
// 'tls' => true,
//'charset' => 'gbk' ) );
$ret = $mail->send();
}
}
add_action('comment_post', 'comment_mail_notify');
function comment_mail_notify($comment_id) {
define('MAIL_SMTP', 'smtp.exmail.qq.com'); //smtp服務(wù)器
define('MAIL_PORT', 25); //smtp端口
define('MAIL_SENDEMAIL', '123456789@qq.com'); //發(fā)送郵件帳號
define('MAIL_PASSWORD', '123456'); //發(fā)送郵件密碼
$admin_notify = '1';
$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 ? '1' : '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") . '回復(fù)被關(guān)注啦~';
$message = '
<div style="width: 502px; height: auto; margin-bottom: 50px; margin-left: auto; margin-right: auto; font-size: 13px; line-height: 14px;">
<div style="width: 502px; margin-top: 10px;">
<div style="font-size: 16px; color: #373737; text-align: center;">'.get_bloginfo("name").'</div>
<div style="font-size: 15px; color: #f0f7eb; padding: 9px; margin-top: 20px; overflow: hidden; background: #299982; padding-left: 30px; padding-right: 40px;">你在 '. get_the_title($comment->comment_post_ID) .' 的評論有了回復(fù):</div>
</div>
<div style="width: 420px; margin-top: 30px; padding: 0 40px 20px; border-left: 1px dashed #299982; border-right: 1px dashed #299982; color: rgba(0,0,0,0.7); background: #f9f9f9; overflow: hidden;">
<div class="one origin" style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0;"><span style="color: #299982;">'. trim(get_comment($parent_id)->comment_author) .'</span>:'. trim(get_comment($parent_id)->comment_content) .'</div>
<div class="one reply" style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0 1em 60px;"><span style="color: #299982;">'. trim($comment->comment_author) .'</span>:'. trim($comment->comment_content) .'</div>
<p style="margin-bottom: 10px;">點擊<a href="' . htmlspecialchars(get_comment_link($parent_id)) . ' style=">查看完整內(nèi)容</a></p>
<p style="margin-bottom: 10px;">(此郵件由系統(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";
$mail = new SaeMail(); //對象
$mail->setOpt(array( 'from' => 'admin@xtwind.com', 'to' => trim($to),//接收信箱
'smtp_host' => MAIL_SMTP , //host
'smtp_port' => MAIL_PORT, //port
'smtp_username' => MAIL_SENDEMAIL,
'smtp_password' => MAIL_PASSWORD,
'subject' => $subject,
'content' => $message,
'content_type' => 'HTML'
// 'tls' => true,
//'charset' => 'gbk' ) );
$ret = $mail->send();
}
}
add_action('comment_post', 'comment_mail_notify');
如果使用上面的不行,可以看看前面這篇文章:
《php使用SAE原生Mail類實現(xiàn)各種類型郵件發(fā)送的方法》
希望本文所述對大家基于wordpress的網(wǎng)站建設(shè)有所幫助。
相關(guān)文章
WordPress實現(xiàn)評論后可顯示內(nèi)容中附件下載地址的方法
這篇文章主要介紹了WordPress實現(xiàn)評論后可顯示內(nèi)容中附件下載地址的方法,可實現(xiàn)用戶在評論后顯示內(nèi)容中附件下載地址的功能,涉及wordpress針對數(shù)據(jù)庫的相關(guān)操作與判定技巧,2016-10-10- 這篇文章主要介紹了WordPress中刪除垃圾評論的方法,需要的朋友可以參考下2015-01-15
WordPress后臺顯示相關(guān)用戶文章相關(guān)聯(lián)評論的方法
這篇文章主要為大家介紹了WordPress后臺顯示相關(guān)用戶文章相關(guān)聯(lián)評論的方法,通過增加自定義函數(shù)SQL調(diào)用來實現(xiàn)顯示相關(guān)評論的功能,非常具有實用價值,需要的朋友可以參考下2014-12-25- 這篇文章主要為大家介紹了wordpress主題評論中添加回復(fù)的方法,可以無需通過插件來實現(xiàn)增加評論回復(fù)功能,是非常實用的技巧,需要的朋友可以參考下2014-12-20
- 這篇文章主要為大家介紹了WordPress屏蔽評論中鏈接地址的方法,可通過自定義函數(shù)進行正則替換刪除鏈接,也可增加nofollow來實現(xiàn)優(yōu)化效果,需要的朋友可以參考下2014-12-18
- 本文主要用到了WordPress功能函數(shù)Query_post()的一種高級用法,就是獲取本周或當(dāng)月或最近30天評論最多的一定數(shù)量的日志,需要的朋友可以參考下2014-07-20
WordPress實現(xiàn)評論提交后跳轉(zhuǎn)的方法
這篇文章主要為大家介紹了WordPress實現(xiàn)評論提交后跳轉(zhuǎn)的方法,需要的朋友可以參考下2014-07-10- 很多WordPress站長都飽受垃圾評論的自擾,苦不堪言。這篇文章主要為大家介紹了WordPress實現(xiàn)自動拒絕垃圾評論的方法,需要的朋友可以參考下2014-06-24
- WordPress點擊評論者鏈接是在本窗口內(nèi)打開,用戶體驗不是特別好,介紹一下wordpress評論者鏈接在新窗口中打開的方法,解決方法如下2014-01-26
WordPress評論郵件通知無插件實現(xiàn)思路及代碼
通常我們喜歡使用Wordpress Thread Comment 或 Mail To Commenter這兩款插件前者久未更新,已測支持WordPress2.9.2版本 其他的未測試,優(yōu)點,不進垃圾箱,支持嵌套回復(fù),今2013-01-14