https://prefect.io logo
m

Michelle Wu

11/18/2020, 8:34 AM
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

Jim Crist-Harif

11/18/2020, 11:26 PM
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

Michelle Wu

11/23/2020, 1:57 AM
@Jim Crist-Harif It worked perfectly, thank you!