|
25 | 25 | from airflow.notifications.basenotifier import BaseNotifier |
26 | 26 | from airflow.providers.smtp.hooks.smtp import SmtpHook |
27 | 27 |
|
| 28 | +try: |
| 29 | + from airflow.settings import SMTP_DEFAULT_TEMPLATED_HTML_CONTENT_PATH, SMTP_DEFAULT_TEMPLATED_SUBJECT |
| 30 | +except ImportError: |
| 31 | + # This is a fallback for when the settings are not available - they were only added in 2.8.1, |
| 32 | + # so we should be able to remove it when min airflow version for the SMTP provider is 2.9.0 |
| 33 | + # we do not raise deprecation warning here, because the user might be using 2.8.0 and the new provider |
| 34 | + # deliberately, and we do not want to upgrade to newer version of Airflow so we should not raise the |
| 35 | + # deprecation warning here. If the user will modify the settings in local_settings even for earlier |
| 36 | + # versions of Airflow, they will be properly used as they will be imported above |
| 37 | + SMTP_DEFAULT_TEMPLATED_HTML_CONTENT_PATH = (Path(__file__).parent / "templates" / "email.html").as_posix() |
| 38 | + SMTP_DEFAULT_TEMPLATED_SUBJECT = """ |
| 39 | +{% if ti is defined %} |
| 40 | +DAG {{ ti.dag_id }} - Task {{ ti.task_id }} - Run ID {{ ti.run_id }} in State {{ ti.state }} |
| 41 | +{% elif slas is defined %} |
| 42 | +SLA Missed for DAG {{ dag.dag_id }} - Task {{ slas[0].task_id }} |
| 43 | +{% endif %} |
| 44 | +""" |
| 45 | + |
28 | 46 |
|
29 | 47 | class SmtpNotifier(BaseNotifier): |
30 | 48 | """ |
@@ -82,8 +100,6 @@ def __init__( |
82 | 100 | *, |
83 | 101 | template: str | None = None, |
84 | 102 | ): |
85 | | - from airflow.settings import SMTP_DEFAULT_TEMPLATED_HTML_CONTENT_PATH, SMTP_DEFAULT_TEMPLATED_SUBJECT |
86 | | - |
87 | 103 | super().__init__() |
88 | 104 | self.smtp_conn_id = smtp_conn_id |
89 | 105 | self.from_email = from_email or conf.get("smtp", "smtp_mail_from") |
|
0 commit comments