Eason09053360 opened a new pull request, #73228:
URL: https://github.com/apache/airflow/pull/73228

   ## Why
   
   On a Dag with `render_template_as_native_obj=True`, `email_on_failure` / 
`email_on_retry` alerts are not delivered.
   
   Those alerts are rendered through the Dag's Jinja environment, so a native 
environment evaluates the rendered subject and body back into Python objects. 
The email backend cannot send a non-string subject or body — `MIMEText` raises 
`AttributeError: 'int' object has no attribute 'encode'` — and 
`_send_error_email_notification` swallows that exception into a log line, so 
the alert disappears with no signal to whoever was supposed to receive it.
   
   This was reported and fixed once as #22152 / #22770, which introduced 
`DAG.get_template_env(force_sandboxed=...)` for exactly this case. It regressed 
in #57354, when the alert path moved to `SmtpNotifier` and the only production 
caller of `force_sandboxed=True` was dropped; the parameter has had no caller 
outside tests since.
   
   ## What
   
   `_send_error_email_notification` in 
`task-sdk/src/airflow/sdk/execution_time/task_runner.py` now renders the alert 
subject and body itself through a sandboxed environment, and removes those two 
fields from the notifier's own templating pass so they are not rendered a 
second time.
   
   The override is deliberately scoped to those two fields rather than applied 
to the whole notifier: recipient fields still render the way the Dag asks for, 
so a templated address list such as `email="{{ var.json.oncall_emails }}"` 
still resolves to a real list instead of being flattened into a string and 
split into invalid addresses. 
`test_alert_recipients_still_render_as_native_objects` pins that down.
   
   One behavior change worth a reviewer's attention: the notifier is now handed 
already-rendered strings for `subject` and `html_content` instead of raw 
templates. This restores the pre-#57354 contract, and the existing assertions 
in `TestEmailNotifications` were updated from the raw template text to the 
rendered output.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes — Claude Code (Opus 5)
   
   Generated-by: Claude Code (Opus 5) following [the 
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to