17713433920 info@mac163.com

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

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

方法/步骤

下载评论自动发邮件功能必需的文件:PHPMailer,将下载下来的PHPMailer压缩包进行解压;

将解压出来的PHPMailer文件夹通过FTP软件上传到自己使用的主题文件夹下;Wordpress网站实现评论自动发邮件功能

将以下的代码复制到自己的模板函数文件functions.php里。

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');

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


微信二维码

微信扫描二维码联系我们!
我们在微信上24小时期待你的声音
提供外贸路由器设备产品,轻松翻墙,解答:WP主题推荐,WP网站建设,Google SEO,百度SEO,专业服务器环境搭建等!


需要提供WordPress主题/插件的汉化服务可以随时联系我们!另外成品WordPress网站以及半成品WordPress网站建设,海外Google SEO优化托管服务,百度SEO优化托管服务,Centos/Debian服务器WP专用环境搭建,WP缓存服务器搭建,我们都是你的首选,拥有多年WP开源程序服务经验,我们一直在坚持客户体验,没有最好,只有更好!
回到顶部