Skip to content

Commit b318f0e

Browse files
committed
Fix backwards compatibility for SMTP provider
The #36226 introduced backwards compatibility check for the smtp provider, where it relied pn airflow settings containig new settings. This PR fixes it by falling back to default settings in case the settings cannot be imported.
1 parent b52b227 commit b318f0e

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

  • airflow/providers/smtp/notifications

airflow/providers/smtp/notifications/smtp.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@
2525
from airflow.notifications.basenotifier import BaseNotifier
2626
from airflow.providers.smtp.hooks.smtp import SmtpHook
2727

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+
2846

2947
class SmtpNotifier(BaseNotifier):
3048
"""
@@ -82,8 +100,6 @@ def __init__(
82100
*,
83101
template: str | None = None,
84102
):
85-
from airflow.settings import SMTP_DEFAULT_TEMPLATED_HTML_CONTENT_PATH, SMTP_DEFAULT_TEMPLATED_SUBJECT
86-
87103
super().__init__()
88104
self.smtp_conn_id = smtp_conn_id
89105
self.from_email = from_email or conf.get("smtp", "smtp_mail_from")

0 commit comments

Comments
 (0)