Skip to content

Airflow: Custom Failure Email Notification Template

Airflow allows you to set custom email notification template in case if you think the default template is not enough. However, this is only for the failure notification and not for retry notification (atleast in 1.10 version, things might change in version 2).

The template is divided into two parts, one for email subject and another for email body.

Default Failure Notification

Default Airflow Failure Notification Email

Custom Failure Email Notification

Custom Airflow Failure Notification Email

Airflow Failure Notification Subject Template

Airflow Job Failed: {{ ti.dag_id }}

Airflow Failure Notification Content Template

<!DOCTYPE html>
<html>
<body>
 <div style=" position: relative;  min-height: 100vh;">
   <div style="padding-bottom: 2.5rem;">
      <b>Task ID:</b> {{ ti.task_id }} <br>
      <b>Task execution date:</b> {{ ti.execution_date }} <br>
      <b>Log:</b> <a href="{{ti.log_url}}">Link</a><br>
      <b>Try:</b> {{try_number}} out of {{max_tries + 1}}<br>
      <b>Exception:</b><p style="background-color: #eee; border: 1px solid #999;display: block;padding: 20px;">{{exception_html}}</p>
      <b>Host:</b> {{ti.hostname}}<br>
      <b>Log file:</b> {{ti.log_filepath}}<br>
      <b>Mark success:</b> <a href="{{ti.mark_success_url}}">Link</a><br>
   </div>
  </br></br></br></br></br></br></br></br></br></br></br></br></br></br></br></br>
   <p style="background-color: #eee; border: 1px solid #999;display: block;padding: 15px;"><small>Powerdby airflow created by gaurang</small></p>
 </div>
</body>
</html>

Changing Config

Finally you need to change the airflow.cfg file to use the new template we just created. Put the above two files so any location and update the email section of the config to add html_content_template and subject_template properties.

[email]
email_backend = airflow.utils.email.send_email_smtp
html_content_template=/root/airflow/email/templates/content_template.j2
subject_template=/root/airflow/email/templates/subject_template.j2
Published inAirflow

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *