Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airflow - how to make EmailOperator html_content dynamic?

Tags:

airflow

I'm looking for a method that will allow the content of the emails sent by a given EmailOperator task to be set dynamically. Ideally I would like to make the email contents dependent on the results of an xcom call, preferably through the html_content argument.

alert = EmailOperator(
    task_id=alertTaskID,
    to='[email protected]',
    subject='Airflow processing report',
    html_content='raw content #2',
    dag=dag
)

I notice that the Airflow docs say that xcom calls can be embedded in templates. Perhaps there is a way to formulate an xcom pull using a template on a specified task ID then pass the result in as html_content? Thanks

like image 951
Ziggy Eunicien Avatar asked Jan 18 '16 18:01

Ziggy Eunicien


1 Answers

Use PythonOperator + send_email instead:

from airflow.operators import PythonOperator
from airflow.utils.email import send_email


def email_callback(**kwargs):
    with open('/path/to.html') as f:
        content = f.read()
    send_email(
        to=[
            # emails
        ],
        subject='subject',
        html_content=content,
    )


email_task = PythonOperator(
    task_id='task_id',
    python_callable=email_callback,
    provide_context=True,
    dag=dag,
)
like image 197
guyskk Avatar answered Jan 01 '23 20:01

guyskk