'Different email list for failure and retries in Airflow

Can there be separate email lists in airflow for retries and failures?

I have the below defaults_args:

default_args = {
'owner': 'airflow',
'start_date': datetime(2022, 1, 30),
'email': ['[email protected]'],
'email_on_failure': True,
'email_on_retry': True
}

In my case, the failure emails should be sent to emails in List A and retry emails should be sent to List B.

Would that be possible?



Solution 1:[1]

Yes, you can use EmailOperator and both on_retry_callback, on_failure_callback to achieve that.

from airflow.operators.email import EmailOperator


def send_email(to, subject, html_content, **context):
    task = EmailOperator(
        to=to,
        subject=subject,
        html_content=html_content
    )
    task.execute(context)


def send_email_on_retry(**context):
    on_retry_email_list = ["List", "of", "your", "recipients"]
    subject = "subject message",
    html_content = "Body of a message"
    send_email(on_retry_email_list, subject, html_content, **context)


def send_email_on_failure(**context):
    on_failure_email_list = ["List", "of", "your", "recipients"]
    subject = "subject message",
    html_content = "Body of a message"
    send_email(on_failure_email_list, subject, html_content, **context)


default_args = {
    'on_failure_callback': send_email_on_failure,
    'on_retry_callback': send_email_on_retry
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 micdum