📖 Scenario: You are setting up an Airflow workflow to send email notifications when a task completes. This helps your team stay informed about important events automatically.
🎯 Goal: Build a simple Airflow DAG that uses the EmailOperator to send an email notification.
📋 What You'll Learn
Create a DAG with the id email_notification_dag
Use EmailOperator to send an email
Set the email recipient to team@example.com
Set the email subject to Airflow Notification
Set the email body to Task completed successfully.
Schedule the DAG to run once daily
💡 Why This Matters
🌍 Real World
Automating email notifications in workflows helps teams stay updated without manual checks.
💼 Career
Knowing how to use EmailOperator is useful for DevOps engineers and data engineers managing Airflow pipelines.
Progress0 / 4 steps
1
Create the DAG structure
Import DAG from airflow and create a DAG object called email_notification_dag with start_date set to 2024, 1, 1 and schedule_interval set to @daily.
Apache Airflow
Hint
Use DAG constructor with the exact parameters given.
2
Import EmailOperator and set email parameters
Import EmailOperator from airflow.operators.email. Create variables to_email with value 'team@example.com', email_subject with value 'Airflow Notification', and email_body with value 'Task completed successfully.'.
Apache Airflow
Hint
Remember to import EmailOperator and create the three variables exactly as shown.
3
Create the EmailOperator task
Create an EmailOperator task called send_email inside the DAG email_notification_dag. Use the variables to_email, email_subject, and email_body for the to, subject, and html_content parameters respectively.
Apache Airflow
Hint
Create the EmailOperator with the exact parameters and variable names.
4
Print confirmation message
Write a print statement that outputs the text 'EmailOperator task created successfully.'
Apache Airflow
Hint
Use print with the exact message inside quotes.
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
Step 1: Understand EmailOperator's role
EmailOperator is designed to send emails triggered by Airflow tasks.
Step 2: Compare with other options
Scheduling tasks, storing logs, and monitoring CPU are handled by other Airflow components, not EmailOperator.
Final Answer:
To send emails automatically from Airflow tasks -> Option D
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
Step 1: Recall correct import path
The correct import path for EmailOperator in Airflow is from airflow.operators.email_operator import EmailOperator.
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.
Final Answer:
from airflow.operators.email_operator import EmailOperator -> Option C
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
Step 1: Check EmailOperator parameters
The 'to' parameter accepts a list, and 'subject' with spaces is valid.
Step 2: Identify common failure cause
EmailOperator requires SMTP server setup in Airflow config; missing this causes failure.
Final Answer:
SMTP server is not configured in Airflow settings -> Option A
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
Step 1: Understand trigger_rule usage
Setting trigger_rule="one_failed" makes the task run only if any upstream task fails.
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.
Final Answer:
Set trigger_rule="one_failed" on the EmailOperator task -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.