3

So I am REALLY new to Airflow, DAGs, the whole nine.

So I have a task where I need to email some data to a list of recipients.

Right now, I have a python function which will send emails, and I was gonna assign this function to a task in my DAG.

However, in the task I was given, I was told that an admin should be able to trigger the DAG, such that additional recipients can receive the data by passing their emails through the airflow configuration json. I am assuming this means, airflow.cfg.

I have been googling for advice, but it seems like most people set up the email section in their airflow.cfg file to just send them an email if there's an error in the dag, not to a list of recipients.

I want to know:

-If I pass a list of email recipients through my airflow.cfg file, can I still assign this email sending process to a task in the DAG?

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
nick_rinaldi
  • 641
  • 6
  • 17

1 Answers1

2

You should use params, which is a dictionary that can be defined at DAG level parameters and remains accesible in every task. This way you can pass the list of recipients, and then use the EmailOperator to send the emails.

From the UI:

trigger_dag_with_params

from airflow.operators.email_operator import EmailOperator

    email_task = EmailOperator(
        to='{{ params.mail_recipients }}',
        task_id='email_task',
        subject='My subject',
        html_content=" Templated HTML content - Today is {{ ds }}"
    )

Remember to previously configure SMTP block in Airflow settings. Check this answer for details and examples using params.

Hope that works for you!

NicoE
  • 4,373
  • 3
  • 18
  • 33