dramis opened a new issue, #49920:
URL: https://github.com/apache/airflow/issues/49920
### Apache Airflow Provider(s)
smtp
### Versions of Apache Airflow Providers
apache-airflow-providers-smtp 2.0.3
### Apache Airflow version
3.0.0
### Operating System
debian 12
### Deployment
Virtualenv installation
### Deployment details
New airflow install on a new server
### What happened
Got a error message sending email on dag succes:
airflow.exceptions.AirflowException: The smtp connection should be loaded
I correct this problem with a custom notifier bases on
airflow.providers.smtp.notifications.smtp import SmtpNotifier,:
```
def notify(self, context):
with SmtpHook(smtp_conn_id=self.smtp_conn_id) as smtp_hook:
"""Send a email via smtp server."""
fields_to_re_render = []
if self.from_email is None:
if smtp_hook.from_email is not None:
self.from_email = smtp_hook.from_email
else:
raise ValueError("You should provide `from_email` or
define it in the connection")
fields_to_re_render.append("from_email")
if self.subject is None:
smtp_default_templated_subject_path: str
if smtp_hook.subject_template:
smtp_default_templated_subject_path =
smtp_hook.subject_template
else:
smtp_default_templated_subject_path = (
Path(__file__).parent / "templates" /
"email_subject.jinja2"
).as_posix()
self.subject =
self._read_template(smtp_default_templated_subject_path)
fields_to_re_render.append("subject")
if self.html_content is None:
smtp_default_templated_html_content_path: str
if smtp_hook.html_content_template:
smtp_default_templated_html_content_path =
smtp_hook.html_content_template
else:
smtp_default_templated_html_content_path = (
Path(__file__).parent / "templates" / "email.html"
).as_posix()
self.html_content =
self._read_template(smtp_default_templated_html_content_path)
fields_to_re_render.append("html_content")
if fields_to_re_render:
jinja_env = self.get_template_env(dag=context["dag"])
self._do_render_template_fields(self, fields_to_re_render,
context, jinja_env, set())
smtp_hook.send_email_smtp(
smtp_conn_id=self.smtp_conn_id,
from_email=self.from_email,
to=self.to,
subject=self.subject,
html_content=self.html_content,
files=self.files,
cc=self.cc,
bcc=self.bcc,
mime_subtype=self.mime_subtype,
mime_charset=self.mime_charset,
custom_headers=self.custom_headers,
)
```
after that i got this error:
```
UndefinedError: 'airflow.sdk.execution_time.task_runner.RuntimeTaskInstance
object' has no attribute 'state'
File
"/opt/airflow/venv/lib/python3.11/site-packages/airflow/sdk/bases/notifier.py",
line 105 in __call__
File
"/opt/airflow/venv/lib/python3.11/site-packages/smtp_thehub_provider/notifications/smtp.py",
line 142 in notify
File
"/opt/airflow/venv/lib/python3.11/site-packages/airflow/sdk/definitions/_internal/templater.py",
line 121 in _do_render_template_fields
File
"/opt/airflow/venv/lib/python3.11/site-packages/airflow/sdk/definitions/_internal/templater.py",
line 177 in render_template
File
"/opt/airflow/venv/lib/python3.11/site-packages/airflow/sdk/bases/notifier.py",
line 57 in _render
File
"/opt/airflow/venv/lib/python3.11/site-packages/airflow/sdk/definitions/_internal/templater.py",
line 133 in _render
File
"/opt/airflow/venv/lib/python3.11/site-packages/airflow/utils/helpers.py", line
244 in render_template_to_string
File
"/opt/airflow/venv/lib/python3.11/site-packages/airflow/utils/helpers.py", line
239 in render_template
File "<template>", line 29 in root
File "/opt/airflow/venv/lib/python3.11/site-packages/jinja2/runtime.py",
line 859 in _fail_with_undefined_error
```
### What you think should happen instead
No error message and the email is sent
### How to reproduce
Run this dag after configure smtp_default connection:
```
from airflow import DAG
from airflow.providers.standard.operators.empty import EmptyOperator
from airflow.providers.smtp.notifications.smtp import SmtpNotifier
from datetime import datetime
default_args = {
"owner": "airflow",
}
with DAG(
dag_id="airflow_email_test",
start_date=datetime(2024, 1, 1),
schedule="@daily",
catchup=False,
default_args=default_args,
tags=["maintenance", "cleanup"],
) as dag:
clean_logs = EmptyOperator(
task_id="empty_operator",
on_success_callback=SmtpNotifier(from_email='[email protected],
to="[email protected]"),
)
```
### Anything else
_No response_
### Are you willing to submit PR?
- [ ] Yes I am willing to submit a PR!
### Code of Conduct
- [x] I agree to follow this project's [Code of
Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)
--
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]