0
0
Apache Airflowdevops~5 mins

EmailOperator for notifications in Apache Airflow - Commands & Configuration

Choose your learning style9 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.