Hey guys. I’ve been running into problems with `Em...
# prefect-community
m
Hey guys. I’ve been running into problems with
EmailTask
in
state_handlers
. I want to receive a email message that shows
task
and
new_state
when failures occur. My handler looks something like this:
Copy code
def email_on_failure(task, old_state, new_state):
    if new_state.is_failed():
         task = EmailTask(
            subject="Prefect alert: {}".format(new_state),
            msg = "Task: {}; New State: {}".format(task, new_state),
            email_from='xxx',
            email_to='xxx',
            smtp_server='smtp.gmail.com',
            smtp_port=xxx,
            smtp_type='STARTTLS'
        ).run()
In tests, I can always receive the email with the correct subject, but the
task
and the
new_state
was always missing in the
msg
…Anyone has encountered similar problems?
j
hmmm, I wonder if this has to do with html interpreting the
<>
brackets on the state as something? Try using
html.escape
to fix this:
Copy code
import html

def email_on_failure(task, old_state, new_state):
    if new_state.is_failed():
         task = EmailTask(
            subject="Prefect alert: {}".format(new_state),
            msg = html.escape("Task: {}; New State: {}".format(task, new_state)),
            email_from='xxx',
            email_to='xxx',
            smtp_server='<http://smtp.gmail.com|smtp.gmail.com>',
            smtp_port=xxx,
            smtp_type='STARTTLS'
        ).run()
m
@Jim Crist-Harif It worked perfectly, thank you!