还不会邮箱模板回复?博客实现评论回复邮箱提醒[WordPress]

网上有很多种邮箱提醒的实例,这里使用用户点击选框允许接收邮箱提醒的一种。并且回复内容为网页模板内容。当然你可以不选择网页内容采用普通文字但过于简陋。

首先,读入HTML邮箱模板

网上有许多的HTML邮箱模板,当然你也可以自己写一个
在这里插入图片描述

读入HTML模板这里代码思路是:
1.首先需要事先在HTML模板中设置一些特殊字段,即你需要显示邮件内容的地方
2.读入HTML文件内容后,使用str_replace()函数替换上面设置的特殊字段内容为你想要发送邮件的内容

<?php
function mailHtml_read ($info_array){
#读入模板文件
$mail_text=file_get_contents('./myself_page/onewords_out/mail.html');
#keyword特殊字段内容
  $keyword=array('/-title-h1/','/-instruction-h2/','/-square-text/','/-main-text-p1/','/-main-text-p2/','/-square-text-2/');
 #替换的邮件内容
  $changeword=array(  $info_array['title-h1'],
                        $info_array['instruction-h2'],
                        $info_array['square-text'],
                        $info_array['main-text-p1'],
                        $info_array['main-text-p2'],
                        $info_array['square-text-2'],
                        );
   $res_html=str_replace($keyword, $changeword, $mail_text, $i); 
    return($res_html);
}
?>

然后实现发送邮件功能

发送邮箱的功能使用WordPress的内置函数wp_mail(),由于国内一些限制,你需要现在WordPress下载相关插件,更改邮箱你才能使用这个函数成功发送邮件

<?php
/* 开始*/
#将下方‘你的邮箱‘处更改为你的邮箱地址
function comment_mail_notify($comment_id) {
#相关变量
  $admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
  $admin_email = '你的邮箱'; // $admin_email 可改为你指定的 e-mail.
  $comment = get_comment($comment_id);
  $comment_author_email = trim($comment->comment_author_email);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  #数据库操作判断条件
  global $wpdb;
  if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
    $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
  if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
    	$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
 		$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
  		$spam_confirmed = $comment->comment_approved;
  if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
	#邮箱相关信息
    $wp_email = '你的邮箱' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
    $info_arrays=array(
                        
                            "title-h1"         =>   "Here is HUImy-梦苑!",    
                            "instruction-h2"   =>   trim(get_comment($parent_id)->comment_author)."--您好",
                            "square-text"      =>   "<b>".get_the_title($comment->comment_post_ID)."--您曾在这里留言:<br /><b/>".trim(get_comment($parent_id)->comment_content),
                            "main-text-p1"     =>   trim($comment->comment_author).' 给您的回复:<br />'.trim($comment->comment_content).'<br /></p><p>您可以点击下发文章链接查看回复的完整內容</p> ',
                            "main-text-p2"     =>   "文章链接:".get_permalink($comment->comment_post_ID),
                            "square-text-2"    =>   "梦想庄园!<p>(此邮件由系统自动发送,请勿回复.)</p>"
                       );
    #读入HTML更换后的HTML模板
    $rehtml=mailHtml_read ($info_arrays) ;           
    $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
   $headers="Content-Type: text/html";
   $mailinfo=wp_mail( $to, $subject, $rehtml, $headers );
  }
}
add_action('comment_post', 'comment_mail_notify');
/* 自动加勾选栏 */
function add_checkbox() {
  echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回复时邮件通知我</label>';
}
add_action('comment_form', 'add_checkbox');

?>

将面的代码复制到主题的function.php下稍微做下更改就可以了

注意事项

在进行测试的时候,如果想要看到反馈可以采用fopen和fwrite函数进行写出文件日志,不要使用php输出alter提示框。因为在WordPress发表评论后会自动刷新,alter无法显示出来

一个热爱且文学式程序猿的博客: 里面有更多精彩内容 HUIMY博客http://blog.huimy.top 博客文章地址;https://blog.huimy.top/17/596.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hui-梦苑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值