西数超哥博客
运维经验教程分享

WordPress网站实现评论自动发邮件功能

很多学建网站学员使用wordpress程序建好了网站,都在自己的网站上添加评论功能。为了让评论者更快的知道自己的评论得到了回复,我们可以给自己网站的评论添加评论自动发邮件功能。(相关教程:wordpress建站教程)

方法/步骤

  1. 下载评论自动发邮件功能必需的文件:PHPMailer,将下载下来的PHPMailer压缩包进行解压;
  2. 将解压出来的PHPMailer文件夹通过FTP软件上传到自己使用的主题文件夹下;
  3. 将以下的代码复制到自己的模板函数文件functions.php里。
    
    
       
        
         /**
     * WordPress 使用 smtp 发送评论提醒邮件
     * 代码来源:学做网站论坛https://www.xuewangzhan.com/
     */

    function comment_mail_notify_editFromClmao ( $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' ) ) {
            $to = trim (get_comment ( $parent_id ) -> comment_author_email ) ;
            $subject = '您在 [' . get_option ( "blogname" ) . '] 的留言有了回应' ;
            $message = '
                <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
                    <p>'
    . trim (get_comment ( $parent_id ) -> comment_author ) . ', 您好!</p>
                    <p>您曾在《'
    . get_the_title ( $comment -> comment_post_ID ) . '》的留言:<br />'
                        . trim (get_comment ( $parent_id ) -> comment_content ) . '</p>
                    <p>'
    . trim ( $comment -> comment_author ) . ' 给您的回应:<br />'
                            . trim ( $comment -> comment_content ) . '<br /></p>
                    <p>您可以点击 <a href="'
    . htmlspecialchars (get_comment_link ( $parent_id ) ) . '">查看完整的回应内容</a></p>
                    <p>欢迎再度光临 <a href="'
    . get_option ( 'home' ) . '">' . get_option ( 'blogname' ) . '</a></p>
                    <p>(此邮件由系统发出, 请勿回复.)</p>
                </div>'
    ;
            header ( "content-type:text/html;charset=utf-8" ) ;
            ini_set ( "magic_quotes_runtime" , 0 ) ;
            require get_template_directory ( ) . '/PHPMailer/class.phpmailer.php' ;
            try {
                $mail = new PHPMailer ( true ) ;
                $mail -> IsSMTP ( ) ;
                $mail -> CharSet = 'UTF-8' ;
                $mail -> SMTPAuth = true ;
                $mail -> Port = 25 ;
                $mail -> Host = "smtp.163.com" ; //邮箱smtp地址,此处以163为例
                $mail -> Username = "你的邮箱账号" ; //你的邮箱账号
                $mail -> Password = "你的邮箱密码" ; //你的邮箱密码
                $mail -> From = "你的邮箱账号" ; //你的邮箱账号
                $mail -> FromName = get_option ( 'blogname' ) ;
                $to = $to ;
                $mail -> AddAddress ( $to ) ;
                $mail -> Subject = $subject ;
                $mail -> Body = $message ;
                $mail -> WordWrap = 80 ;
                //$mail->AddAttachment("f:/test.png"); //可以添加附件
                $mail -> IsHTML ( true ) ;
                $mail -> Send ( ) ;
                } catch (phpmailerException $e ) {
                // echo "邮件发送失败:".$e->errorMessage(); //测试的时候可以去掉此行的注释
            }
        }
    }
    add_action ( 'comment_post' , 'comment_mail_notify_editFromClmao' ) ;
  4. 修改以上代码里的邮箱地址和邮箱密码,建议使用189邮箱。189邮箱注册使用方法参考:https://www.xuewangzhan.com/dz/18008.html

通过以上的操作,我们就可以实现自己的网站拥有评论自动发邮件功能,用户的评论得到回复后,系统会自动发邮件给用户,大大增加了网站的用户体验,有利于SEO优化排名。效果如下图:

www.ysidc.top 西数超哥博客,IT技术,idc资讯,基础运维,原创教程,web环境部署,WordPress教程,技术分享,LAMP,LNMP,wdcp,mysql,mssql,centos,discuz教程

赞(0)
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除。本站原创内容未经允许不得转载:西数超哥博客 » WordPress网站实现评论自动发邮件功能