A-A+
wordpress纯代码实现评论回复邮件通知
wordpress 本身是可以进行对评论用户的邮件回复的,但其默认使用的是 mail() 函数,如果空间商不给打开 mail() 函数的话就没有办法将回复的内容发送到访客的邮箱,访客也就不能第一时间看到站点发送的邮件了。
除了评论回复邮件通知外,需要使用邮件的还有找回密码,注册会员等,如果空间没有开启 mail() 函数,这些功能都是无法使用的,下面夏日博客就来教大家一种使用纯代码来实现评论回复邮件通知的功能。
首先需要让 wordpress 支持 SMTP,因为我们要使用这种方式来进行邮件的发送,代码如下:
//smtp add_action('phpmailer_init', 'mail_smtp'); function mail_smtp( $phpmailer ) { $phpmailer->FromName = '夏日博客(xiariboke)'; //发信人名称 $phpmailer->Host = 'smtp.exmail.qq.com'; //smtp服务器 $phpmailer->Port = 465; //端口 $phpmailer->Username = 'admin@xiariboke.net';//邮箱帐号 $phpmailer->Password = '********';//邮箱密码 $phpmailer->From = 'admin@xiariboke.net'; //邮箱帐号 $phpmailer->SMTPAuth = true; $phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25留空,465为ssl) $phpmailer->IsSMTP(); }
代码中的注释都已经写好了,在使用 SMTP 发送邮件的时候要先把 SMTP 服务器需要设置的参数设置成自己的,如果不会设置的话直接到相应邮件商邮件设置里面去查看,一般都有详细的说明的,把这段代码放入到主题 functions.php 文件中,这个时候 wordpress 已经支持 SMTP 发送邮件了,我们还需要进行邮件的发送,让 wordpress 评论后进行自动邮件的回复吧,代码如下:
//评论回复邮件 function comment_mail_notify($comment_id){ $comment = get_comment($comment_id); $parent_id = $comment->comment_parent ? $comment->comment_parent : ''; $spam_confirmed = $comment->comment_approved; if(($parent_id != '') && ($spam_confirmed != 'spam')){ $wp_email = 'webmaster@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); $to = trim(get_comment($parent_id)->comment_author_email); $subject = '你在 [' . get_option("blogname") . '] 的留言有了回应'; $message = ' <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-style: solid; border-width: 1;" bordercolor="#85C1DF" width="700" height="251" id="AutoNumber1" align="center"> <tr> <td width="520" height="28" bgcolor="#F5F9FB"><font color="#1A65B6" style="font-size:14px"> <b>留言回复通知 | <a href="https://www.xiariboke.net" targe="blank">xiariboke(夏日博客)</a></b></font></td> </tr> <tr> <td width="800" height="210" bgcolor="#FFffff" valign="top" style=" padding:8px;"> <span class="STYLE2"><strong >' . trim(get_comment($parent_id)->comment_author) . '</strong>, 你好!</span> <p> <span class="STYLE2">你曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br /> --->' . trim(get_comment($parent_id)->comment_content) . '<br /><br /> ' . trim($comment->comment_author) . ' 给你的回复:<br /> --->' . trim($comment->comment_content) . '</span></p> <p > <Strong>你可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复完整内容</a></Strong></p> <p><span class="STYLE2"> <strong>感谢你对 <a href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a> 的关注,欢迎<a href="http://list.qq.com/cgi-bin/qf_invite?id=3077e5391b4997f56a6854122b0c351be607d83c3ebf649b" target="_blank">订阅本站</a></strong>!</p> <p> 更多内容请:<a href="https://www.xiariboke.net" target="_blank">夏日博客</a> | <a href="http://wpa.qq.com/msgrd?v=3&uin=543886382&site=xiariboke&menu=yes">联系站长</a> | <a href="mailto:xiariboke@qq.com">投稿</a> | <a href="https://www.xiariboke.net/tougao/">投稿须知</a> | <a href="https://www.xiariboke.net/about/">关于我们</a> | <a href="https://www.xiariboke.net/contact/">联系我们</a></p></td> </tr> <tr> <td width="800" height="16" bgcolor="#85C1DF" bordercolor="#008000"><div align="center"><font color="#fff"><a href="https://www.xiariboke.net">夏日博客(www.xiariboke.net)</a></font></div></td> </tr> </table>'; $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');