Skip to content

Commit ae6eeeb

Browse files
committed
Improve SmtpNotifier default email templates for readability
- Improve subject line to show DAG.task and run context clearly - Add new email_content.jinja2 HTML template with dark theme matching Airflow UI - Display task details (DAG, task, run ID, attempt, state, host) in structured grid - Conditional action buttons for View Log and Mark Success (Airflow 3.x safe) - Handle both task failure (ti context) and SLA missed context - Remove task-sdk templates and revert task_runner.py (email belongs in smtp provider) Closes apache#61667
1 parent 2f911d0 commit ae6eeeb

6 files changed

Lines changed: 147 additions & 194 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{#
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an
12+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
KIND, either express or implied. See the License for the
14+
specific language governing permissions and limitations
15+
under the License.
16+
#}
17+
<!DOCTYPE html>
18+
<html lang="en">
19+
<head>
20+
<meta charset="UTF-8" />
21+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
22+
<title>Airflow Notification</title>
23+
<style>
24+
body { margin: 0; padding: 0; background-color: #0d1117; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; color: #e6edf3; }
25+
.wrapper { max-width: 680px; margin: 32px auto; background-color: #161b22; border: 1px solid #30363d; border-radius: 8px; overflow: hidden; }
26+
.header-failure { background-color: #da3633; padding: 20px 28px; }
27+
.header-sla { background-color: #e3b341; padding: 20px 28px; }
28+
.header-failure h1, .header-sla h1 { margin: 0; font-size: 18px; font-weight: 600; color: #ffffff; }
29+
.header-failure p, .header-sla p { margin: 4px 0 0; font-size: 13px; color: rgba(255,255,255,0.85); }
30+
.body { padding: 24px 28px; }
31+
.section-title { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.8px; color: #8b949e; margin: 0 0 12px; }
32+
.details-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 24px; }
33+
.detail-card { background-color: #0d1117; border: 1px solid #30363d; border-radius: 6px; padding: 12px 14px; }
34+
.detail-card .label { font-size: 11px; color: #8b949e; margin-bottom: 4px; }
35+
.detail-card .value { font-size: 14px; font-weight: 500; color: #e6edf3; word-break: break-all; }
36+
.exception-block { background-color: #0d1117; border: 1px solid #f85149; border-left: 3px solid #f85149; border-radius: 6px; padding: 14px 16px; margin-bottom: 24px; }
37+
.exception-block pre { margin: 0; font-size: 12px; color: #f85149; white-space: pre-wrap; word-break: break-word; font-family: 'SF Mono', 'Fira Code', monospace; line-height: 1.5; }
38+
.sla-block { background-color: #0d1117; border: 1px solid #e3b341; border-left: 3px solid #e3b341; border-radius: 6px; padding: 14px 16px; margin-bottom: 24px; }
39+
.sla-block pre { margin: 0; font-size: 12px; color: #e3b341; white-space: pre-wrap; word-break: break-word; font-family: 'SF Mono', 'Fira Code', monospace; line-height: 1.5; }
40+
.actions { display: flex; gap: 12px; margin-top: 4px; flex-wrap: wrap; }
41+
.btn { display: inline-block; padding: 9px 18px; border-radius: 6px; font-size: 13px; font-weight: 500; text-decoration: none; }
42+
.btn-primary { background-color: #1f6feb; color: #ffffff; }
43+
.btn-secondary { background-color: #21262d; color: #c9d1d9; border: 1px solid #30363d; }
44+
.footer { background-color: #0d1117; border-top: 1px solid #30363d; padding: 14px 28px; font-size: 12px; color: #8b949e; }
45+
</style>
46+
</head>
47+
<body>
48+
<div class="wrapper">
49+
50+
{% if ti is defined %}
51+
<div class="header-failure">
52+
<h1>&#9888; Task Failure Alert</h1>
53+
<p>{{ ti.dag_id }} &rsaquo; {{ ti.task_id }}</p>
54+
</div>
55+
<div class="body">
56+
<p class="section-title">Task Details</p>
57+
<div class="details-grid">
58+
<div class="detail-card">
59+
<div class="label">DAG</div>
60+
<div class="value">{{ ti.dag_id }}</div>
61+
</div>
62+
<div class="detail-card">
63+
<div class="label">Task</div>
64+
<div class="value">{{ ti.task_id }}</div>
65+
</div>
66+
<div class="detail-card">
67+
<div class="label">Run ID</div>
68+
<div class="value">{{ ti.run_id }}</div>
69+
</div>
70+
<div class="detail-card">
71+
<div class="label">Attempt</div>
72+
<div class="value">{{ ti.try_number }} of {{ ti.max_tries + 1 }}</div>
73+
</div>
74+
<div class="detail-card">
75+
<div class="label">State</div>
76+
<div class="value">{{ ti.state }}</div>
77+
</div>
78+
<div class="detail-card">
79+
<div class="label">Host</div>
80+
<div class="value">{{ ti.hostname }}</div>
81+
</div>
82+
</div>
83+
84+
<div class="actions">
85+
{% if ti.log_url %}
86+
<a href="{{ ti.log_url }}" class="btn btn-primary">View Log</a>
87+
{% endif %}
88+
{% if ti.mark_success_url is defined and ti.mark_success_url %}
89+
<a href="{{ ti.mark_success_url }}" class="btn btn-secondary">Mark Success</a>
90+
{% endif %}
91+
</div>
92+
</div>
93+
94+
{% elif slas is defined %}
95+
<div class="header-sla">
96+
<h1>&#8987; SLA Missed</h1>
97+
<p>{{ dag.dag_id }}</p>
98+
</div>
99+
<div class="body">
100+
<p class="section-title">SLA Details</p>
101+
<div class="details-grid">
102+
<div class="detail-card">
103+
<div class="label">DAG</div>
104+
<div class="value">{{ dag.dag_id }}</div>
105+
</div>
106+
<div class="detail-card">
107+
<div class="label">Tasks</div>
108+
<div class="value">{{ task_list }}</div>
109+
</div>
110+
<div class="detail-card">
111+
<div class="label">Blocking Tasks</div>
112+
<div class="value">{{ blocking_task_list }}</div>
113+
</div>
114+
</div>
115+
116+
<p class="section-title">SLA Misses</p>
117+
<div class="sla-block">
118+
<pre>{{ slas }}</pre>
119+
</div>
120+
121+
<p class="section-title">Blocking Task Instances</p>
122+
<div class="sla-block">
123+
<pre>{{ blocking_tis }}</pre>
124+
</div>
125+
</div>
126+
{% endif %}
127+
128+
<div class="footer">
129+
Sent by Apache Airflow
130+
{% if ti is defined %}&mdash; {{ ti.hostname }}{% endif %}
131+
</div>
132+
</div>
133+
</body>
134+
</html>

‎providers/smtp/src/airflow/providers/smtp/notifications/templates/email_subject.jinja2‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
to you under the Apache License, Version 2.0 (the
77
"License"); you may not use this file except in compliance
88
with the License. You may obtain a copy of the License at
9-
109
http://www.apache.org/licenses/LICENSE-2.0
11-
1210
Unless required by applicable law or agreed to in writing,
1311
software distributed under the License is distributed on an
1412
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -17,7 +15,7 @@
1715
under the License.
1816
#}
1917
{% if ti is defined %}
20-
DAG {{ ti.dag_id }} - Task {{ ti.task_id }} - Run ID {{ ti.run_id }} in State {{ ti.state }}
18+
[Airflow] Task Failed: {{ ti.dag_id }}.{{ ti.task_id }} | Run: {{ ti.run_id }}
2119
{% elif slas is defined %}
22-
SLA Missed for DAG {{ dag.dag_id }} - Task {{ slas[0].task_id }}
20+
[Airflow] SLA Missed: {{ dag.dag_id }}.{{ slas[0].task_id }}
2321
{% endif %}

‎task-sdk/src/airflow/sdk/execution_time/task_runner.py‎

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,39 +1573,25 @@ def _send_error_email_notification(
15731573
if subject_template_file and Path(subject_template_file).exists():
15741574
subject = Path(subject_template_file).read_text()
15751575
else:
1576-
# Try to load from built-in template files
1577-
default_subject_template = (
1578-
Path(__file__).parent.parent / "templates" / "email" / "task_failure_subject.txt"
1579-
)
1580-
if default_subject_template.exists():
1581-
subject = default_subject_template.read_text()
1582-
else:
1583-
# Final fallback to inline default for backward compatibility
1584-
subject = "Airflow alert: {{ti}}"
1576+
# Fallback to default
1577+
subject = "Airflow alert: {{ti}}"
15851578

15861579
html_content_template_file = conf.get("email", "html_content_template", fallback=None)
15871580

15881581
# Read the template file if configured
15891582
if html_content_template_file and Path(html_content_template_file).exists():
15901583
html_content = Path(html_content_template_file).read_text()
15911584
else:
1592-
# Try to load from built-in template files
1593-
default_html_template = (
1594-
Path(__file__).parent.parent / "templates" / "email" / "task_failure_body.html"
1585+
# Fallback to default
1586+
# For reporting purposes, we report based on 1-indexed,
1587+
# not 0-indexed lists (i.e. Try 1 instead of Try 0 for the first attempt).
1588+
html_content = (
1589+
"Try {{try_number}} out of {{max_tries + 1}}<br>"
1590+
"Exception:<br>{{exception_html}}<br>"
1591+
'Log: <a href="{{ti.log_url}}">Link</a><br>'
1592+
"Host: {{ti.hostname}}<br>"
1593+
'Mark success: <a href="{{ti.mark_success_url}}">Link</a><br>'
15951594
)
1596-
if default_html_template.exists():
1597-
html_content = default_html_template.read_text()
1598-
else:
1599-
# Final fallback to inline default for backward compatibility
1600-
# For reporting purposes, we report based on 1-indexed,
1601-
# not 0-indexed lists (i.e. Try 1 instead of Try 0 for the first attempt).
1602-
html_content = (
1603-
"Try {{try_number}} out of {{max_tries + 1}}<br>"
1604-
"Exception:<br>{{exception_html}}<br>"
1605-
'Log: <a href="{{ti.log_url}}">Link</a><br>'
1606-
"Host: {{ti.hostname}}<br>"
1607-
'Mark success: <a href="{{ti.mark_success_url}}">Link</a><br>'
1608-
)
16091595

16101596
# Add exception_html to context for template rendering
16111597
import html

‎task-sdk/src/airflow/sdk/templates/email/task_failure_body.html‎

Lines changed: 0 additions & 113 deletions
This file was deleted.

‎task-sdk/src/airflow/sdk/templates/email/task_failure_body.txt‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎task-sdk/src/airflow/sdk/templates/email/task_failure_subject.txt‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)