Avinash Kumar
Avinash Kumar

Reputation: 101

Success mail in Airflow

How can I get success mail in airflow after complete execution of a each dags.

This is what I have tried. I am tring to get a success mail on completion of dags Can anyone will help me out. I have import all the files that are required.

i = datetime.now()

default_args = {
'owner': 'owner',
'depeneds_on_past': False,
'start_date': datetime(i.year, i.month, i.day-1),
'email': ['[email protected]'],
'email_on_failure': True,
'email_on_retry': True,
'email_on_success': True,
'retries': 0,
'retry_delay': timedelta(minutes=10)
}

dag = DAG('update', default_args = default_args, 
schedule_interval="0 3 * * *")


t0 = PythonOperator(
task_id='clear',
python_callable=empty_tables,
email_on_failure=True,
email_on_success=True,
email=['[email protected]'],
dag=dag
)

# Add tasks now 
t1 = BashOperator(
task_id='export',
bash_command=script,
dag=dag
)


t2 = PythonOperator(
task_id='load',
python_callable=load,
email_on_failure=True,
email_on_success=True,
email=['[email protected]'],
dag=dag
)



t0 >> t1 >> t2

Upvotes: 9

Views: 20277

Answers (3)

lulu
lulu

Reputation: 341

sending emails using jinja templates in python
Here's the DAG and how to use on_success_callback

def email_func():
    # https://sites.google.com/site/mrxpalmeiras/python/send-email-using-jinja-template?fbclid=IwAR2rnllu2rb0PhbE7pc-BGOlhGoTZKp74t4H8NKZrmuwlNlBmrChI15Zd5U
    # I'm referring to send_email from the above package/code
    send_email(to, subject)

def callable_func(context):
    to = "[email protected]"
    subject = 'on success callback trial'
    html = "/path/to/html"
    email_func(to, subject, html)

default_args = {
    'owner': 'Airflow',
    'depends_on_past': False,
    'start_date': datetime(2020, 9, 15),
    'retries': 1,
    'email': ['your_email@your_domain.com'],
    'email_on_failure': True,
    'email_on_retry': True,
    'on_success_callback': callable_func,
}

Upvotes: 2

IshitaV
IshitaV

Reputation: 73

While there exists 'email_on_failure' key to raise alerts in case of failed task, there isn't any key defined in airflow code as 'email_on_success'. Hence, in order to trigger an email alert on success, you would have to make use of 'on_success_callback'

Upvotes: 3

kaxil
kaxil

Reputation: 18844

You need to configure SMTP server and add this to airflow.cfg file.

Check https://stackoverflow.com/a/51837049/5691525 to see how you can setup SMTP Server for Airflow Email alerts using Gmail.

Upvotes: 4

Related Questions