B2私信邮箱通知代码

7b24周前更新 admin
13 0

7B2 主题的私信邮件提醒功能,意思就是当用户发送私信时,接收方会收到邮件通知。这项功能确保即使用户没有实时查看站点,也不会错过重要的私信消息。具体效果如下图所示:
修改教程

B2私信邮箱通知代码
在进行修改之前,请务必备份原始文件。修改路径:b2/Modules/Common/RestApi.php,找到第 2721 行的代码,删除或注释掉该行代码及其注释部分

  1. ppublic staticfunction sendDirectmessage($request)
  2. {
  3. if(!b2_check_repo())
  4. returnnew \WP_Error('user_error', __('操作频次过高','b2'), array('status'=>403));
  5. $res =Directmessage::send_directmessage($request['user_id'], $request['content']);
  6. if(isset($res['error'])){
  7. returnnew \WP_Error('dmsg_error', $res['error'], array('status'=>403));
  8. }else{
  9. returnnew \WP_REST_Response($res,200);
  10. }
  11. }

修改为以下代码:(注意不要弄错)

  1. publicstaticfunction sendDirectmessage($request)
  2. {
  3. if(!b2_check_repo()){
  4. returnnew \WP_Error('user_error', __('操作频次过高','b2'), array('status'=>403));
  5. }
  6. // 获取当前用户ID
  7. $user_id = $request['user_id'];
  8. // 假设您有一个函数来获取当前登录用户的ID和信息(这里需要您自己实现或调整)
  9. // 例如:global $current_user; get_currentuserinfo(); (这是旧的方法,WordPress新版本可能不同)
  10. // 在WordPress REST API中,您可能需要通过其他方式获取当前用户,比如通过认证令牌
  11. // 这里我们暂时假设$current_user_id是已经获取到的当前登录用户的ID
  12. // 注意:这个变量在您的原始代码中不存在,需要根据您的实际环境进行获取
  13. $current_user_id = b2_get_current_user_id();// 这是一个假设的函数,您需要根据实际情况替换
  14. $current_user = get_userdata($current_user_id);// 获取当前用户信息
  15. // 获取当前时间戳的小时部分
  16. $current_hour = date('Hi');
  17. // 获取用户在当前时间窗口内的邮件发送次数
  18. $email_count = wp_cache_get("email_count_{$user_id}_{$current_hour}",'direct_message');
  19. if($email_count ===false){
  20. $email_count =0;
  21. }
  22. // 检查是否已达到发送限制
  23. if($email_count >= MAX_EMAILS_PER_HOUR){
  24. returnnew \WP_Error('email_limit_error', __('您已达到每小时的私信或邮件发送限制。','b2'), array('status'=>429));
  25. }
  26. // 发送私信
  27. $res =Directmessage::send_directmessage($user_id, $request['content']);
  28. if(isset($res['error'])){
  29. // 如果私信发送失败,返回错误
  30. returnnew \WP_Error('dmsg_error', $res['error'], array('status'=>403));
  31. }else{
  32. // 准备邮件通知(如果用户有邮箱)
  33. $user_info = get_userdata($user_id);
  34. if($user_info && $user_info->user_email){
  35. // 更新邮件发送次数
  36. $email_count++;
  37. wp_cache_set("email_count_{$user_id}_{$current_hour}", $email_count,'direct_message', TIME_WINDOW_HOURS *3600);// 设置缓存有效期为1小时
  38. // 准备邮件内容
  39. $subject ='您有一条新的私信';
  40. $message = sprintf(
  41. '<div style="max-width: 700px; background-color: #fff; margin: 0 auto; border: 1px solid #e3e3e3; font-family: Arial, sans-serif; color: #333; border-radius: 8px;">
  42. <!-- Header Section with Text Logo and Gradient Background -->
  43. <div style="height: 80px; line-height: 80px; border-bottom: 1px solid #e3e3e3; padding: 0 30px; background: linear-gradient(135deg, #2c3e50, #4e2a84); border-top-left-radius: 8px; border-top-right-radius: 8px; text-align: center;">
  44. <a href="https://js.vmccc.com" style="display: block; text-decoration: none; font-size: 34px; color: #fff; letter-spacing: 4px;" target="_blank" rel="noopener">
  45. 技术巢
  46. </a>
  47. </div>
  48. <!-- Body Section -->
  49. <div style="padding: 30px; line-height: 1.6; background-color: #fafafa;">
  50. <p style="font-size: 16px; color: #555; margin-bottom: 10px;">
  51. <strong>%1$s</strong>,您收到一条私信,发送人:
  52. <span style="color:#0066ff;"><a href="#" style="color:#0066ff; text-decoration:none;" target="_blank" rel="noopener">%2$s</a></span>,私信内容:
  53. </p>
  54. <!-- Message Content with Icon -->
  55. <div style="padding: 15px; background-color: #e3f7ff; border-left: 5px solid #66c2ff; border-radius: 5px; margin-top: 15px;">
  56. <img src="https://js.vmccc.com/wp-content/uploads/2025011104392289.png" alt="Message Icon" style="width: 32px; vertical-align: middle; margin-right: 10px;">
  57. <span style="font-size: 18px; color: #333;">%3$s</span>
  58. </div>
  59. <!-- Warning Section with Icon -->
  60. <div style="margin-top: 25px; padding: 15px; background-color: #fff3cd; border-left: 5px solid #ffec3d; border-radius: 5px;">
  61. <a href="%4$s" style="color:#0066ff; text-decoration:none; font-weight: bold;">点击这里 · 进入技术巢 </a> 查看详情。
  62. </div>
  63. <!-- Footer Section -->
  64. <p style="font-size: 14px; color: #888; margin-top: 30px; text-align: center;">
  65. 我们无法辨别私信内容的真实性,请仔细甄别!
  66. </p>
  67. </div>
  68. <!-- Footer Notice with Icon -->
  69. <div style="font-size: 12px; color: #999; border-top: 1px dotted #E3E3E3; padding: 15px; text-align: center; background-color: #f5f5f5; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
  70. <img src="https://img.icons8.com/ios/50/000000/email.png" alt="Email Icon" style="width: 20px; vertical-align: middle; margin-right: 2px;">
  71. 本邮件为系统邮件,请勿回复。
  72. </div>
  73. </div>',
  74. esc_html($user_info->display_name),
  75. esc_html($current_user->display_name),// 发送者名称
  76. wp_kses_post($request['content']),// 私信内容
  77. esc_url(home_url('/')),// 网站URL
  78. esc_html(get_bloginfo('name'))// 网站名称
  79. );
  80. // 发送邮件
  81. $headers = array('Content-Type: text/html; charset=UTF-8');
  82. wp_mail($user_info->user_email, $subject, $message, $headers);
  83. }
  84. // 无论是否发送邮件,都返回私信发送成功的响应
  85. // 注意:这里可能需要根据您的业务逻辑进行调整,比如是否需要在响应中包含邮件发送状态
  86. returnnew \WP_REST_Response($res,200);
  87. }
  88. }

同时,在主题的 functions.php 文件中添加以下代码:

  1. define('MAX_EMAILS_PER_HOUR',5);// 每小时最多发送的邮件数
  2. define('TIME_WINDOW_HOURS',1);// 限制的时间窗口(小时)
© 版权声明

相关文章

没有相关内容!

暂无评论

none
暂无评论...