Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I attach a file on email operator in Airflow

Tags:

airflow

Whenever I add the files argument to the email_task I get a failed run.

email_task = EmailOperator(    
           task_id='email_sample_data',    
           to='[email protected]',    
           subject='Forecast for the day',    
           html_content= "Sample",
           files=['/home/airflow/sample.html'],    
           dag=dag)

I'm getting an error that the file is not found. Where does airflow pick my file, where do I need to upload a file, and what is the correct syntax for the 'files' argument?

like image 562
Spazz Avatar asked Jun 30 '21 19:06

Spazz


Video Answer


1 Answers

Airflow expect path to be relative to where the DAG file is stored. However since files is templated field you can use template_search_path to provide additional paths that Airflow will look in:

with DAG(
        ...
        template_searchpath = ['/home/airflow/'],
        ) as dag:
    email_task = EmailOperator(
        task_id='email_sample_data',
        to='[email protected]',
        subject='Forecast for the day',
        html_content="Sample",
        files=['/home/airflow/sample.html']
    )
like image 95
Elad Kalif Avatar answered Oct 16 '22 15:10

Elad Kalif