WordPress网站实现评论自动发邮件功能 |
您所在的位置:网站首页 › 自动发评论 › WordPress网站实现评论自动发邮件功能 |
很多学建网站学员使用wordpress程序建好了网站,都在自己的网站上添加评论功能。为了让评论者更快的知道自己的评论得到了回复,我们可以给自己网站的评论添加评论自动发邮件功能。(相关教程:wordpress建站教程) 方法/步骤 下载评论自动发邮件功能必需的文件:PHPMailer,将下载下来的PHPMailer压缩包进行解压;将解压出来的PHPMailer文件夹通过FTP软件上传到自己使用的主题文件夹下;将以下的代码复制到自己的模板函数文件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 = '' . trim (get_comment ( $parent_id ) -> comment_author ) . ', 您好! 您曾在《' . get_the_title ( $comment -> comment_post_ID ) . '》的留言:' . trim (get_comment ( $parent_id ) -> comment_content ) . ' ' . trim ( $comment -> comment_author ) . ' 给您的回应:' . trim ( $comment -> comment_content ) . ' 您可以点击 查看完整的回应内容 欢迎再度光临 ' . get_option ( 'blogname' ) . ' (此邮件由系统发出, 请勿回复.) ' ; 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' ) ; 修改以上代码里的邮箱地址和邮箱密码,建议使用189邮箱。189邮箱注册使用方法参考:https://www.xuewangzhan.com/dz/18008.html通过以上的操作,我们就可以实现自己的网站拥有评论自动发邮件功能,用户的评论得到回复后,系统会自动发邮件给用户,大大增加了网站的用户体验,有利于SEO优化排名。效果如下图: www.ysidc.top 云速博客,IT技术,idc资讯,基础运维,原创教程,web环境部署,WordPress教程,技术分享,LAMP,LNMP,wdcp,mysql,mssql,centos,discuz教程 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |