本文實(shí)例講述了WordPress實(shí)現(xiàn)回復(fù)文章評(píng)論后發(fā)送郵件通知的功能。分享給大家供大家參考,具體如下:
很多時(shí)候,人們都希望在自己的評(píng)論被管理員回復(fù)后會(huì)收到通知。該函數(shù)的作用就是回復(fù)后自動(dòng)郵件通知評(píng)論者。
把下面的代碼加到wordpress的主題函數(shù)里面,然后修改下郵件帳號(hào)密碼。
該函數(shù)是針對(duì)SAE平臺(tái)的wordpress,非SAE平臺(tái)不能使用,有需要的話留言我也會(huì)寫出相應(yīng)方法。
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ā)送郵件帳號(hào)
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 = '
點(diǎn)擊查看完整內(nèi)容
(此郵件由系統(tǒng)發(fā)出,無需回復(fù).)
';
$from = "From: \"" . get_option('blogname') . "\" ";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
$mail = new SaeMail(); //對(duì)象
$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類實(shí)現(xiàn)各種類型郵件發(fā)送的方法》文章來源:http://www.zghlxwxcb.cn/news/detail-423271.html
希望本文所述對(duì)大家基于wordpress的網(wǎng)站建設(shè)有所幫助。文章來源地址http://www.zghlxwxcb.cn/news/detail-423271.html
到了這里,關(guān)于WordPress實(shí)現(xiàn)回復(fù)文章評(píng)論后發(fā)送郵件通知的功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!