How do I attach a file on email operator in Airflow

Viewed 891

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

email_task = EmailOperator(    
           task_id='email_sample_data',    
           to='sample@sample.com',    
           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?

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='sample@sample.com',
        subject='Forecast for the day',
        html_content="Sample",
        files=['/home/airflow/sample.html']
    )
Related