爱听故事的人 發表於 2023-8-24 00:00:00

WordPress 有人回复时邮件通知的实现方法 SAE 移植版

<p>
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>1,先配置一下您的SMTP账户的用户名和密码。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>2,将下面的内容粘贴到当前主题的 functions.php 中即可,注意别破坏了原来的结构就行。(或者直接建一个文件,将代码拷贝进去,然后在 functions.php 中引用该文件即可。) </span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php <br>
/** <br>
* Email Me With The Replay For SAE <br>
* <br>
* Usage:将此段代码粘贴到当前主题的 functions.php 文件中即可。 <br>
* 或者将此文件放进主题目录,在 functions.php 中包含此文件 <br>
* <br>
* 基于 comment_mail_notify v1.0 by willin kan. http://kan.willin.org/?p=1295(已失效) <br>
* changelist <br>
* 2012-03-29 <br>
* version 0.1 <br>
* <br>
* 2012-04-29 <br>
* version 0.2 <br>
* edit $subject to this code <br>
* $subject = '您的评论 ' . mb_substr(strip_tags(get_comment($parent_id)-&gt;comment_content), 0, 10, 'utf-8') . ' 有了回应 - ' . get_option("blogname"); <br>
* <br>
* <br>
* @author IT不倒翁, willin kan <br>
* @copyright (C) 2011,2012 Just Use It! <br>
* @link yungbo.com <br>
* @version 0.2 <br>
*/ <br>
function comment_mail_notify($comment_id) { <br>
//换行标志 <br>
$lineBreak = "\n"; <br>
//缩进标志 <br>
$tabBreak = "\t"; <br>
$admin_notify = '1'; // are you willing to receive the email? 1 is yes. <br>
$admin_email = get_bloginfo('admin_email'); // you can change $admin_email to your e-mail optionaly. <br>
$comment = get_comment($comment_id); <br>
$comment_author_email = trim($comment-&gt;comment_author_email); <br>
$parent_id = $comment-&gt;comment_parent ? $comment-&gt;comment_parent : ''; <br>
global $wpdb; <br>
if ($wpdb-&gt;query("Describe {$wpdb-&gt;comments} comment_mail_notify") == '') <br>
$wpdb-&gt;query("ALTER TABLE {$wpdb-&gt;comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;"); <br>
if (($comment_author_email != $admin_email &amp;&amp; isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email &amp;&amp; $admin_notify == '1')) <br>
$wpdb-&gt;query("UPDATE {$wpdb-&gt;comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'"); <br>
$notify = $parent_id ? get_comment($parent_id)-&gt;comment_mail_notify : '0'; <br>
$spam_confirmed = $comment-&gt;comment_approved; <br>
if ($parent_id != '' &amp;&amp; $spam_confirmed != 'spam' &amp;&amp; $notify == '1') { <br>
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail <br>
$to = trim(get_comment($parent_id)-&gt;comment_author_email); <br>
$subject = '您的评论 ' . mb_substr(strip_tags(get_comment($parent_id)-&gt;comment_content), 0, 10, 'utf-8') . ' 有了回应 - ' . get_option("blogname"); <br>
//邮件主体内容 <br>
//估计新浪考虑到安全问题,全部用 htmlspecialchars() 过滤了,所以不要使用 html 标记 <br>
$message = '您好,' . trim(get_comment($parent_id)-&gt;comment_author) . $lineBreak; <br>
$message .= '您曾在 ' . get_option("blogname") . ' 留言:' . $lineBreak; <br>
$message .= $tabBreak . trim(get_comment($parent_id)-&gt;comment_content) . $lineBreak; <br>
$message .= trim($comment-&gt;comment_author) . ' 回复了您:' . $lineBreak; <br>
$message .= $tabBreak . trim($comment-&gt;comment_content) . $lineBreak; <br>
$message .= '您可以点击 ' . get_comment_link($parent_id) . ' 查看具体情况' . $lineBreak; <br>
$message .= $lineBreak; <br>
$message .= '(系统邮件,请不要直接回复)' . $lineBreak; <br>
$message .= get_option('blogname') . $lineBreak; <br>
$message .= home_url() . $lineBreak; <br>
/*SAE 配置位置*/ <br>
$saeTo = $to; <br>
$saeSubject = $subject; <br>
$saeMessage = $message; <br>
$saeSMTPUser = '';//SMTP邮箱 <br>
$saeSMTPPass = '';//SMTP密码 <br>
$mail = new SaeMail(); <br>
$result = $mail-&gt;quickSend($saeTo, $saeSubject, $saeMessage, $saeSMTPUser, $saeSMTPPass); <br>
// if ($result === false) <br>
// var_dump($mail-&gt;errno(), $mail-&gt;errmsg()); <br>
} <br>
} <br>
add_action('comment_post', 'comment_mail_notify'); <br>
/* Auto checked */ <br>
function add_checkbox() { <br>
echo '&lt;input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" /&gt;&lt;label for="comment_mail_notify"&gt;有人回复我时邮件通知我&lt;/label&gt;'; <br>
} <br>
add_action('comment_form', 'add_checkbox'); </p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>代码最近更新日期,2012-04-29</span></p>
頁: [1]
查看完整版本: WordPress 有人回复时邮件通知的实现方法 SAE 移植版