感恩的闫 發表於 2023-12-23 00:00:00

WordPress实现回复文章评论后发送邮件通知的功能

<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
本文实例讲述了WordPress实现回复文章评论后发送邮件通知的功能。分享给大家供大家参考,具体如下:</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
很多时候,人们都希望在自己的评论被管理员回复后会收到通知。该函数的作用就是回复后自动邮件通知评论者。</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
把下面的代码加到wordpress的主题函数里面,然后修改下邮件帐号密码。</p>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
该函数是针对SAE平台的wordpress,非SAE平台不能使用,有需要的话留言我也会写出相应方法。<br>
 </p>
<div style="margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; font-size: 12px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden;">
<div 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>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode5" style="margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; 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;">
//邮件回复<br>
function comment_mail_notify($comment_id) {<br>
define('MAIL_SMTP', 'smtp.exmail.qq.com'); //smtp服务器<br>
define('MAIL_PORT', 25); //smtp端口<br>
define('MAIL_SENDEMAIL', '123456789@qq.com'); //发送邮件帐号<br>
define('MAIL_PASSWORD', '123456'); //发送邮件密码<br>
$admin_notify = '1';<br>
$admin_email = get_bloginfo ('admin_email');<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 ? '1' : '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']));<br>
$to = trim(get_comment($parent_id)-&gt;comment_author_email);<br>
$subject = '你在' . get_option("blogname") . '回复被关注啦~';<br>
$message = '<br>
&lt;div style="width: 502px; height: auto; margin-bottom: 50px; margin-left: auto; margin-right: auto; font-size: 13px; line-height: 14px;"&gt;<br>
&lt;div style="width: 502px; margin-top: 10px;"&gt;<br>
&lt;div style="font-size: 16px; color: #373737; text-align: center;"&gt;'.get_bloginfo("name").'&lt;/div&gt;<br>
&lt;div style="font-size: 15px; color: #f0f7eb; padding: 9px; margin-top: 20px; overflow: hidden; background: #299982; padding-left: 30px; padding-right: 40px;"&gt;你在 '. get_the_title($comment-&amp;gt;comment_post_ID) .' 的评论有了回复:&lt;/div&gt;<br>
&lt;/div&gt;<br>
&lt;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;"&gt;<br>
&lt;div style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0;"&gt;&lt;span style="color: #299982;"&gt;'. trim(get_comment($parent_id)-&amp;gt;comment_author) .'&lt;/span&gt;:'. trim(get_comment($parent_id)-&amp;gt;comment_content) .'&lt;/div&gt;<br>
&lt;div style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0 1em 60px;"&gt;&lt;span style="color: #299982;"&gt;'. trim($comment-&amp;gt;comment_author) .'&lt;/span&gt;:'. trim($comment-&amp;gt;comment_content) .'&lt;/div&gt;<br>
&lt;p style="margin-bottom: 10px;"&gt;点击&lt;a href="' . htmlspecialchars(get_comment_link($parent_id)) . ' style="&gt;查看完整内容&lt;/a&gt;&lt;/p&gt;<br>
&lt;p style="margin-bottom: 10px;"&gt;(此邮件由系统发出,无需回复.)&lt;/p&gt;<br>
&lt;/div&gt;<br>
&lt;/div&gt;<br>
';<br>
$from = "From: \"" . get_option('blogname') . "\" &lt;$wp_email&gt;";<br>
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";<br>
$mail = new SaeMail(); //对象<br>
$mail-&gt;setOpt(array( 'from' =&gt; 'admin@xtwind.com', 'to' =&gt; trim($to),//接收信箱<br>
'smtp_host' =&gt; MAIL_SMTP , //host<br>
'smtp_port' =&gt; MAIL_PORT, //port<br>
'smtp_username' =&gt; MAIL_SENDEMAIL,<br>
'smtp_password' =&gt; MAIL_PASSWORD,<br>
'subject' =&gt; $subject,<br>
'content' =&gt; $message,<br>
'content_type' =&gt; 'HTML'<br>
// 'tls' =&gt; true,<br>
//'charset' =&gt; 'gbk' ) );<br>
$ret = $mail-&gt;send();<br>
}<br>
}<br>
add_action('comment_post', 'comment_mail_notify');</div>
<p style="margin: 0px; padding: 5px 0px; outline: none; line-height: 30px;">
希望本文所述对大家基于wordpress的网站建设有所帮助。</p>
<div style="margin: 0px; padding: 0px; outline: none; line-height: 25.2px;">
 </div>
<div style="margin: 8px 0px 10px; padding: 0px; outline: none; width: 700px; overflow: hidden;">
<ul style='margin: 0px; padding-right: 0px; padding-left: 0px; outline: none; list-style: none; max-width: 500px; line-height: 12px; font-family: tahoma, arial, "Microsoft YaHei";'></ul>
</div>
<p>
 </p>
頁: [1]
查看完整版本: WordPress实现回复文章评论后发送邮件通知的功能