Bird
Raised Fist0
Apache Airflowdevops~5 mins

EmailOperator for notifications in Apache Airflow - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you want your workflow to send an email to notify someone when a task finishes or fails. EmailOperator in Airflow helps you send emails automatically as part of your workflow.
When you want to send a success notification email after a data pipeline finishes.
When you want to alert your team by email if a task fails in your workflow.
When you want to send daily reports automatically via email from your workflow.
When you want to include logs or results in an email after a task completes.
When you want to automate reminders or status updates by email.
Config File - email_notification_dag.py
email_notification_dag.py
from airflow import DAG
from airflow.operators.email import EmailOperator
from airflow.utils.dates import days_ago

with DAG(
    dag_id='email_notification_dag',
    start_date=days_ago(1),
    schedule_interval='@daily',
    catchup=False
) as dag:

    send_email = EmailOperator(
        task_id='send_email',
        to='team@example.com',
        subject='Airflow Notification: Task Completed',
        html_content='<h3>Your Airflow task has completed successfully!</h3>'
    )

This DAG defines a simple workflow that sends an email notification.

dag_id: The name of the workflow.

start_date: When the workflow starts running.

schedule_interval: Runs daily.

EmailOperator: Sends an email to the specified address with subject and HTML content.

Commands
List all available DAGs to verify your email notification DAG is recognized by Airflow.
Terminal
airflow dags list
Expected OutputExpected
email_notification_dag example_bash_operator example_branch_dop_operator example_xcom
Manually trigger the email notification DAG to run immediately and send the email.
Terminal
airflow dags trigger email_notification_dag
Expected OutputExpected
Created <DagRun email_notification_dag @ 2024-06-01T12:00:00+00:00: manual__2024-06-01T12:00:00+00:00, externally triggered: True>
List all tasks in the email notification DAG to confirm the email task is present.
Terminal
airflow tasks list email_notification_dag
Expected OutputExpected
send_email
Test the send_email task for the given date to see if the email sends correctly without running the whole DAG.
Terminal
airflow tasks test email_notification_dag send_email 2024-06-01
Expected OutputExpected
[2024-06-01 12:00:00,000] {email_operator.py:100} INFO - Sending email to: team@example.com [2024-06-01 12:00:01,000] {email_operator.py:120} INFO - Email sent successfully
Key Concept

If you remember nothing else from this pattern, remember: EmailOperator lets you send emails automatically as part of your Airflow workflows to notify or alert people.

Common Mistakes
Not configuring SMTP settings in Airflow before using EmailOperator
Without SMTP settings, Airflow cannot send emails and the task will fail.
Set SMTP server, port, login, and password in airflow.cfg or environment variables before running EmailOperator.
Using plain text in html_content parameter
html_content expects valid HTML; plain text may not render properly in email clients.
Use simple HTML tags like <h3> or <p> in html_content for better email formatting.
Not specifying the 'to' email address correctly
If the recipient email is missing or malformed, the email will not be sent.
Always provide a valid email address or list of emails in the 'to' parameter.
Summary
Define an Airflow DAG with EmailOperator to send emails automatically.
Use airflow CLI commands to list, trigger, and test the email task.
Ensure SMTP settings are configured so Airflow can send emails successfully.

Practice

(1/5)
1. What is the primary purpose of the EmailOperator in Airflow?
easy
A. To store logs of task executions
B. To schedule tasks at specific times
C. To monitor CPU usage of Airflow workers
D. To send emails automatically from Airflow tasks

Solution

  1. Step 1: Understand EmailOperator's role

    EmailOperator is designed to send emails triggered by Airflow tasks.
  2. Step 2: Compare with other options

    Scheduling tasks, storing logs, and monitoring CPU are handled by other Airflow components, not EmailOperator.
  3. Final Answer:

    To send emails automatically from Airflow tasks -> Option D
  4. Quick Check:

    EmailOperator = send emails [OK]
Hint: EmailOperator is for sending emails, not scheduling [OK]
Common Mistakes:
  • Confusing EmailOperator with scheduling operators
  • Thinking EmailOperator manages logs or monitoring
  • Assuming EmailOperator runs tasks instead of notifying
2. Which of the following is the correct way to import EmailOperator in an Airflow DAG file?
easy
A. from airflow.operators.email_operator import Email
B. from airflow.operators.email import EmailOperator
C. from airflow.operators.email_operator import EmailOperator
D. import EmailOperator from airflow.operators.email_operator

Solution

  1. Step 1: Recall correct import path

    The correct import path for EmailOperator in Airflow is from airflow.operators.email_operator import EmailOperator.
  2. Step 2: Check other options for syntax or naming errors

    from airflow.operators.email import EmailOperator uses wrong module name (missing '_operator'); from airflow.operators.email_operator import Email imports wrong class name; import EmailOperator from airflow.operators.email_operator uses invalid Python import syntax.
  3. Final Answer:

    from airflow.operators.email_operator import EmailOperator -> Option C
  4. Quick Check:

    Correct import = airflow.operators.email_operator [OK]
Hint: Use 'from airflow.operators.email_operator import EmailOperator' [OK]
Common Mistakes:
  • Using wrong module path like email
  • Incorrect import syntax like 'import X from Y'
  • Confusing class name with module name
3. Given this Airflow task using EmailOperator:
email_task = EmailOperator(
    task_id='send_email',
    to='user@example.com',
    subject='Test Email',
    html_content='Hello from Airflow!',
    dag=dag
)
What will happen when this task runs successfully?
medium
A. The task will send an email but with empty content
B. An email with subject 'Test Email' is sent to user@example.com
C. No email is sent because 'to' is not a list
D. The task will fail because html_content is missing

Solution

  1. Step 1: Analyze EmailOperator parameters

    The task specifies 'to' as a string email address, 'subject', and 'html_content' correctly.
  2. Step 2: Understand EmailOperator behavior

    EmailOperator accepts a string or list for 'to' and sends the email with given subject and content.
  3. Final Answer:

    An email with subject 'Test Email' is sent to user@example.com -> Option B
  4. Quick Check:

    Valid params = email sent [OK]
Hint: Single string 'to' is valid for EmailOperator [OK]
Common Mistakes:
  • Thinking 'to' must be a list
  • Assuming html_content is required to be a file
  • Believing task fails without attachments
4. You wrote this EmailOperator task but it fails with an error:
email_task = EmailOperator(
    task_id='notify',
    to=['user@example.com'],
    subject='Alert',
    html_content='Check this alert',
    dag=dag
)
What is the most likely cause of the failure?
medium
A. SMTP server is not configured in Airflow settings
B. The 'to' parameter must be a string, not a list
C. Missing 'from_email' parameter in EmailOperator
D. The 'subject' parameter cannot contain spaces

Solution

  1. Step 1: Check EmailOperator parameters

    The 'to' parameter accepts a list, and 'subject' with spaces is valid.
  2. Step 2: Identify common failure cause

    EmailOperator requires SMTP server setup in Airflow config; missing this causes failure.
  3. Final Answer:

    SMTP server is not configured in Airflow settings -> Option A
  4. Quick Check:

    SMTP setup missing = failure [OK]
Hint: Check SMTP config if EmailOperator fails [OK]
Common Mistakes:
  • Assuming 'to' cannot be a list
  • Thinking the 'subject' parameter cannot contain spaces
  • Thinking 'from_email' is mandatory parameter
5. You want to send an email notification only when a task fails using EmailOperator. Which Airflow feature should you use to trigger the email task only on failure?
hard
A. Set trigger_rule="one_failed" on the EmailOperator task
B. Set trigger_rule="all_success" on the EmailOperator task
C. Use depends_on_past=True on the EmailOperator task
D. Set email_on_failure=False in the DAG definition

Solution

  1. Step 1: Understand trigger_rule usage

    Setting trigger_rule="one_failed" makes the task run only if any upstream task fails.
  2. Step 2: Evaluate other options

    all_success runs only if all succeed; depends_on_past controls task dependency on previous runs; email_on_failure is a DAG-level flag, not for task trigger.
  3. Final Answer:

    Set trigger_rule="one_failed" on the EmailOperator task -> Option A
  4. Quick Check:

    Trigger on failure = trigger_rule one_failed [OK]
Hint: Use trigger_rule='one_failed' to run on failure only [OK]
Common Mistakes:
  • Using trigger_rule='all_success' for failure notification
  • Confusing depends_on_past with failure triggers
  • Misusing email_on_failure flag for task triggers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard