SMTP 通知操作指南¶
简介¶
SMTP 通知器 (airflow.providers.smtp.notifications.smtp.SmtpNotifier
) 允许用户在 DAG 级别和 Task 级别使用各种 on_*_callbacks
向 SMTP 服务器发送消息。
示例代码:¶
from datetime import datetime
from airflow import DAG
from airflow.providers.standard.operators.bash import BashOperator
from airflow.providers.smtp.notifications.smtp import send_smtp_notification
with DAG(
dag_id="smtp_notifier",
schedule=None,
start_date=datetime(2023, 1, 1),
catchup=False,
on_failure_callback=[
send_smtp_notification(
from_email="[email protected]",
to="[email protected]",
subject="[Error] The dag {{ dag.dag_id }} failed",
html_content="debug logs",
)
],
):
BashOperator(
task_id="mytask",
on_failure_callback=[
send_smtp_notification(
from_email="[email protected]",
to="[email protected]",
subject="[Error] The Task {{ ti.task_id }} failed",
html_content="debug logs",
)
],
bash_command="fail",
)