活力39282
在线时间13640 小时
阅读权限200
管理员
自由的灵魂
- 积分
- 107434
- 主题
- 5630
- 回帖
- 26661
- 注册时间
- 2003-4-10
- 最后登录
- 2026-10-2
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
1、问题缘起
论坛已有帖子页面勾选回帖的「管理」操作邮件通知功能,但前台管理面板(modcp)的批量删帖功能一直没有邮件通知。当版主或管理员通过 forum.php?mod=modcp&action=thread&op=post 批量删除回帖时,被删帖用户收不到任何通知,导致用户不知道自己的帖子为何消失,影响用户体验和社区沟通效率。
讨论过程中发现的相关问题:
| 问题 | 解决方案 | | 后台批量删帖代码位置不明确 | 通过 grep 搜索定位到 modcp_thread.php | | 被删除的帖子需要按作者分组 | 遍历 $pidsdelete 数组,按 authorid 分组 | | 需要区分单条和批量删除 | 根据 $post_count 判断,分别构建邮件内容 | | 操作名称需要醒目 | 使用红色 <span> 包裹「删除回帖」 | | 邮件发送不能阻塞删除操作 | 使用现有 common_mail_queue 异步发送 |
2、解决方案
(1)简要技术分析:
在 modcp_thread.php 的批量删除逻辑中,收集被删除的帖子信息,按作者分组后,通过邮件队列异步发送通知邮件。
(2)修改文件列表:
| 操作 | 文件路径 | | 修改 | /source/include/modcp/modcp_thread.php |
(3)关键代码段:
- // ========= 批量删帖邮件通知 =========
- if ($pidsdelete) {
- require_once libfile('function/mail');
-
- // 收集被删除的帖子信息
- $deleted_pids = array();
- foreach ($pidsdelete as $fid => $pids) {
- foreach ($pids as $pid) {
- $deleted_pids[] = $pid;
- }
- }
-
- if (!empty($deleted_pids)) {
- // 获取帖子详情
- $posts = C::t('forum_post')->fetch_all($posttableid, $deleted_pids);
- $tids = array();
- foreach ($posts as $post) {
- $tids[$post['tid']] = $post['tid'];
- }
- $threads = C::t('forum_thread')->fetch_all($tids);
-
- // 按作者分组
- $author_posts = array();
- foreach ($posts as $post) {
- $authorid = $post['authorid'];
- if ($authorid > 0) {
- $author_posts[$authorid][] = $post;
- }
- }
-
- // 发送邮件给每个作者
- foreach ($author_posts as $authorid => $user_posts) {
- $author = C::t('common_member')->fetch($authorid);
- if (empty($author['email'])) continue;
-
- $post_count = count($user_posts);
- $subject = '您在 『' . $_G['forum']['name'] . '』 的回帖被批量删除';
-
- $mail_message = "<html><body>";
- $mail_message .= "<p>尊敬的 " . $author['username'] . ",您好:</p>";
-
- if ($post_count == 1) {
- $post = $user_posts[0];
- $thread = $threads[$post['tid']];
- $mail_message .= "<p>您在主题《" . htmlspecialchars($thread['subject']) . "》中的回帖被执行了「<span style="color:red;">删除回帖</span>」操作。</p>";
- $mail_message .= "<p>查看详情:<a href="" . $_G['siteurl'] . "forum.php?mod=viewthread&tid=" . $post['tid'] . "">" . $_G['siteurl'] . "forum.php?mod=viewthread&tid=" . $post['tid'] . "</a></p>";
- } else {
- $mail_message .= "<p>您在论坛发布的以下 " . $post_count . " 个回帖被执行了「<span style="color:red;">删除回帖</span>」操作:</p><ul>";
- foreach ($user_posts as $post) {
- $thread = $threads[$post['tid']];
- $mail_message .= "<li><a href="" . $_G['siteurl'] . "forum.php?mod=viewthread&tid=" . $post['tid'] . "">" . htmlspecialchars($thread['subject']) . "</a></li>";
- }
- $mail_message .= "</ul>";
- $mail_message .= "<p>操作人:" . $_G['member']['username'] . "</p>";
- $mail_message .= "<p>操作时间:" . date('Y-m-d H:i:s') . "</p>";
- }
- $mail_message .= "<p>登录论坛查看详细操作记录。</p>";
- $mail_message .= "</body></html>";
-
- C::t('common_mail_queue')->insert(array(
- 'uid' => $authorid,
- 'toemail' => $author['email'],
- 'username' => $author['username'],
- 'subject' => $subject,
- 'message' => $mail_message,
- 'dateline' => TIMESTAMP,
- 'status' => 0,
- ));
- }
- }
- }
- // ========= 批量删帖邮件通知结束 =========
复制代码
3、最终实现效果
| 场景 | 邮件内容 | 红色高亮 | | 单条回帖被删除 | 显示具体主题标题和查看链接 | ✅ 「删除回帖」 | | 多条回帖被删除 | 显示所有被删帖子的主题列表 | ✅ 「删除回帖」 | | 跨不同用户删除 | 每个作者独立收到自己的通知 | ✅ | | 删除主题(整帖) | 由现有 send_topicadmin_notification_universal 处理 | — |
4、重要技术沉淀
需更新到固化摘要:
- 十七、前台管理面板批量删帖邮件通知(2026-06-09)
- 功能:版主/管理员通过 modcp 批量删除回帖时,自动发送邮件通知被删帖用户
- 涉及文件:source/include/modcp/modcp_thread.php
- 触发位置:批量删除操作完成后、showmessage 之前
- 邮件格式:HTML,删除操作红色高亮
- 发送方式:异步邮件队列,不阻塞删除操作
复制代码
踩坑点:
| 踩坑 | 正确做法 | | 误以为 admincp_members.php 是批量删帖入口 | 实际在 modcp_thread.php 的 $op == 'post' 分支 | | 删除主题和删除回帖混在一起处理 | 通过 $post['first'] 判断,主题由现有函数处理 | | 邮件发送阻塞删除操作 | 使用 common_mail_queue 异步队列 |
5、进一步优化可能性
- 操作原因支持:前台管理面板目前不需要填写原因,如需增加可在表单中添加 reason 字段
- 合并通知:同一用户短时间内多次被删帖,可合并为一封邮件
- 版块开关:允许版主设置是否发送删除通知
|
|