Airflow 3.2.2默认邮件模板未正确加载问题咨询
Airflow 3 自定义邮件模板不生效问题解决
问题根源
Airflow 3 对通知系统做了模块化重构,原 Airflow 2 中 [email] 段的 subject_template、html_content_template 配置不再被默认的 SmtpNotifier 自动读取,必须显式指定模板参数或通过全局配置绑定。
解决方案
方案1:在回调中显式初始化带模板的 SmtpNotifier
直接在失败回调函数中创建 SmtpNotifier 实例时指定模板路径,替代默认实例:
from airflow.notifications.builtin_notifiers.smtp_notifier import SmtpNotifier def custom_failure_notify(context): # 替换为你的模板绝对路径或相对于 AIRFLOW_HOME 的路径 notifier = SmtpNotifier( subject_template="templates/email/failure_subject.txt", html_content_template="templates/email/failure_html.html" ) notifier.notify(context) # 在 DAG 中使用该回调 with DAG( dag_id="your_dag_id", on_failure_callback=custom_failure_notify, # 其他 DAG 参数 ): # 任务定义
方案2:全局配置默认 SmtpNotifier 参数
修改 airflow.cfg,在 [notifications] 段添加模板配置,全局生效:
[notifications] # 设置默认通知器为 SMTP default_notifier = airflow.notifications.builtin_notifiers.smtp_notifier.SmtpNotifier # 指定主题模板路径 smtp_notifier_subject_template = /opt/airflow/templates/email/failure_subject.txt # 指定HTML内容模板路径 smtp_notifier_html_content_template = /opt/airflow/templates/email/failure_html.html
修改后重启 Airflow 服务,所有使用默认通知器的失败回调都会自动加载自定义模板。
注意事项
- 确保模板文件路径正确,Airflow 进程拥有读取权限;
- 模板语法保持与 Airflow 2 兼容(使用 Jinja2 变量,如
{{ dag.dag_id }}、{{ task_instance.task_id }}等); - 若使用早期 Airflow 3 版本,建议升级到最新小版本修复可能存在的模板加载 bug。
内容的提问来源于stack exchange,提问作者user23449623
相关产品推荐
相关产品推荐

