ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

InvenTree 邮件通知插件(Email Notification Plugin)完整指南:通知发送机制、过滤逻辑与邮件配置

InvenTree 邮件通知插件(Email Notification Plugin)完整指南:通知发送机制、过滤逻辑与邮件配置 InvenTree 邮件通知插件Email Notification Plugin完整指南通知发送机制、过滤逻辑与邮件配置【免费下载链接】InvenTreeOpen Source Inventory Management System项目地址: https://gitcode.com/GitHub_Trending/in/InvenTree本篇技术指南聚焦于 InvenTree 开源库存管理系统中内置的Email Notification Plugin邮件通知插件。该插件基于NotificationMixin通知混合类实现在系统内发生特定事件时自动向用户发送邮件。读完本文你将掌握邮件通知插件的触发与投递链路、用户接收条件已注册邮箱 个人偏好开启、send_notification的底层实现逻辑、以及如何通过配置文件启用 SMTP/ESP 使通知真正送达。插件定位InvenTree 三大内置通知通道之一InvenTree 将“事件发生 → 通知用户”这一能力抽象为NotificationMixin混合类见 NotificationMixin.py。任何由 InvenTree 核心系统产生的通知都可以通过实现了该混合类的插件分发给用户。系统内置了三种通知通道见 notification.md 文档UI Notifications在 InvenTree 界面右上角显示未读通知角标Email Notifications本文主题将通知渲染为邮件发送到用户邮箱Slack Notifications将通知推送到 Slack 频道可选插件需配置 Webhook URL 后启用。三个插件均定义在同一个源文件中core_notifications.py。邮件通知插件的类名为InvenTreeEmailNotifications其关键元信息如下属性值NAMEInvenTreeEmailNotificationsTITLEInvenTree Email NotificationsSLUGinventree-email-notificationAUTHORInvenTree contributorsVERSION1.0.0与 UI 通知插件始终内置生效不同邮件通知是否真正发送同时受“事件是否携带可渲染模板”和“收件人是否满足条件”两层约束下面逐一展开。通知发送的核心接口NotificationMixinNotificationMixin是邮件通知插件的技术底座。该混合类定义于 NotificationMixin.py提供了两个核心方法send_notificationdef send_notification(self, target: Model, category: str, users: list, context: dict) - bool: Send notification to the specified target users. 默认实现不做任何事返回 False。 return False基类默认返回False具体通道由各插件覆写。参数含义target通知关联的模型实例如某个物料、库存项或订单category通知类别字符串users待通知的用户列表context通知上下文数据字典用于渲染消息内容返回值True表示通知发送成功False表示未发送。filter_targetsdef filter_targets(self, targets: list[User]) - list[User]: Filter notification targets based on the plugins logic. 默认实现返回全部目标用户。 return targets如果插件需要更细粒度地控制哪些用户被通知可以实现filter_targets对目标用户列表进行过滤。邮件通知插件的 send_notification 实现剖析InvenTreeEmailNotifications同时继承NotificationMixin与SettingsMixin后者用于提供用户级设置项其send_notification完整实现了“模板渲染 → 用户过滤 → 邮件投递”的流程见 core_notifications.py第 1 步模板校验# Ignore if there is no template provided to render if not context.get(template): return False只有事件上下文context中携带了template键时才会继续。context[template]需包含htmlHTML 模板必填通过 Django 的render_to_string结合context渲染为邮件正文subject邮件主题可选未提供时为空字符串。html_message render_to_string(context[template][html], context) subject context[template].get(subject, )第 2 步实例标题前缀若全局设置INVENTREE_INSTANCE实例名称非空会将其作为前缀拼接到邮件主题中便于用户区分来自不同 InvenTree 实例的通知instance_title get_global_setting(INVENTREE_INSTANCE) if instance_title: subject f[{instance_title}] {subject}例如实例名为MyFactory时主题会变为[MyFactory] 库存低于阈值。第 3 步逐用户过滤与收件人收集for user in users: # Skip if the user does not want to receive email notifications if not self.get_user_setting(NOTIFY_BY_EMAIL, user, backup_valueFalse): continue if email : InvenTree.helpers_email.get_email_for_user(user): recipients.append(email)这是文档所述“只向已注册邮箱地址、且已在个人资料中开启邮件通知的用户发送”的代码实现包含两道关卡用户偏好开关读取每个用户的NOTIFY_BY_EMAIL设置项详见下文“用户级设置”未开启的用户被直接跳过邮箱有效性通过helpers_email.get_email_for_user(user)解析用户邮箱详见下文“邮箱解析顺序”。第 4 步批量投递if recipients: InvenTree.helpers_email.send_email( subject, , recipients, html_messagehtml_message, force_asyncnot settings.TESTING, ) return True return False只要存在至少一个收件人就调用helpers_email.send_email发送没有任何收件人则返回False不发送。force_asyncnot settings.TESTING表明正常生产环境邮件通过后台任务异步发送测试模式下则为同步发送。用户级设置NOTIFY_BY_EMAIL插件通过SettingsMixin的USER_SETTINGS定义了一个用户级布尔设置项见 core_notifications.pyUSER_SETTINGS { NOTIFY_BY_EMAIL: { name: _(Allow email notifications), description: _(Allow email notifications to be sent to this user), default: True, validator: bool, } }字段值说明nameAllow email notifications设置项显示名称descriptionAllow email notifications to be sent to this user设置项说明defaultTrue默认开启邮件通知validatorbool仅接受布尔值每个用户都可以在自己的用户配置中开启或关闭该选项。结合 docs/docs/settings/user.md 中介绍的用户设置管理界面用户可自主决定是否接收系统事件的邮件通知。注意即使该选项开启用户仍然必须拥有一个可用的邮箱地址通知才会真正送达。邮箱解析顺序get_email_for_userInvenTreeEmailNotifications并不直接读取user.email而是委托给InvenTree.helpers_email.get_email_for_user(user)定义于 helpers_email.py解析优先级如下首选user.email字段——若用户账号上直接填写了邮箱直接使用备选查询 django-allauth 的EmailAddress表按-primary是否主邮箱、-verified是否已验证排序后取第一条。if user.email: return user.email if (email : EmailAddress.objects.filter(useruser).order_by(-primary, -verified).first()): return email.email这意味着用户可以通过账号直接绑定邮箱或通过 allauth 添加并验证辅助邮箱二者任一满足即可成为通知收件人。邮件投递链路send_email 与后台任务helpers_email.send_email见 helpers_email.py是 InvenTree 统一的邮件发送入口其执行流程数据导入保护若系统正处于数据导入状态isImportingData()直接返回失败避免导入期间误发邮件邮件配置检查调用is_email_configured()若EMAIL_HOST或DEFAULT_FROM_EMAIL未配置且非测试环境则记录日志INVE-W7: Email server not configured并拒绝发送发件人回退未显式指定from_email时使用settings.DEFAULT_FROM_EMAIL异步投递通过tasks.offload_task(issue_mail, ..., groupnotification)将邮件任务卸载到后台 worker如 Celery异步执行。is_email_configured()同文件第 17-61 行的判定逻辑还包含一个特殊分支若INTERNAL_EMAIL_BACKEND不是 Django 默认的django.core.mail.backends.smtp.EmailBackend例如改用 django-anymail 或测试用的locmem后端则跳过 SMTP 主机检查直接视为已配置。前置条件在 InvenTree 中配置邮件服务邮件通知插件只是“通道”真正的投递依赖 InvenTree 的邮件配置。根据 email.md 文档InvenTree 使用 django-anymail 支持多种 ESP邮件服务商包括 Amazon SES、Brevo、Postal、Mailgun、Postmark、SendGrid 等也可使用通用 SMTP。在 config_template.yaml 对应的配置文件config.yaml中邮件相关配置位于email段典型示例email: backend: SMTP # 可选: SMTP / Console / 其他 django-anymail 后端 host: smtp.example.com # SMTP 服务器地址对应 EMAIL_HOST port: 587 # SMTP 端口 username: userexample.com # SMTP 登录用户名对应 EMAIL_HOST_USER password: secret # SMTP 登录密码对应 EMAIL_HOST_PASSWORD tls: true # 启用 TLS ssl: false sender: noreplyexample.com # 对应 DEFAULT_FROM_EMAIL作为通知邮件发件人关键点EMAIL_HOST与DEFAULT_FROM_EMAIL必须配置否则is_email_configured()返回Falsesend_email会以错误码INVE-W7拒绝投递。此外 InvenTree 还提供EMAIL_ENABLED开关、EMAIL_USE_TLS/EMAIL_USE_SSL、以及面向 ESP 的EMAIL_*系列参数可结合 config.md 中的 “Email Settings” 一节查阅全部选项。从事件到邮件的完整链路结合核心通知代码与邮件辅助模块一次邮件通知的完整调用链为InvenTree 核心系统产生事件如库存告警、订单状态变更、BOM 更新系统确定target关联模型实例、category类别、users目标用户并构造context含template.html、subject、message、link等通知分发框架遍历所有实现NotificationMixin的插件依次调用各自的send_notificationInvenTreeEmailNotifications.send_notification先校验context[template]再渲染 HTML 正文、拼接实例名主题前缀逐用户检查NOTIFY_BY_EMAIL设置并解析邮箱得到有效收件人列表调用helpers_email.send_email通过is_email_configured()校验后将issue_mail任务offload_task到后台 workergroupnotification异步发送。Superuser 可以在管理后台Admin Center查看邮件日志以排查发送失败或追踪投递状态参见 admin.md 中的说明邮件日志默认保留 30 天可在 global.md 全局设置 中调整保留时长。扩展阅读NotificationMixin 混合类文档send_notification/filter_targets接口约定UI Notification Plugin站内通知通道InvenTreeUINotifications批量写入NotificationMessage见 common/models.pySlack Notification PluginSlack 通道InvenTreeSlackNotifications需配置NOTIFICATION_SLACK_URLEmail 配置文档SMTP / ESP 与收发件完整说明核心通知实现源码三大内置通知插件的完整实现邮件发送辅助模块邮箱解析、配置校验与异步投递逻辑。【免费下载链接】InvenTreeOpen Source Inventory Management System项目地址: https://gitcode.com/GitHub_Trending/in/InvenTree创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表