0
0
Apache Airflowdevops~10 mins

EmailOperator for notifications in Apache Airflow - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - EmailOperator for notifications
Start DAG run
Trigger EmailOperator
Prepare email content
Connect to SMTP server
Send email
Email sent successfully?
NoLog error
Yes
End EmailOperator task
This flow shows how Airflow's EmailOperator sends an email notification during a DAG run, connecting to SMTP and handling success or failure.
Execution Sample
Apache Airflow
from airflow.operators.email import EmailOperator
email_task = EmailOperator(
    task_id='send_email',
    to='user@example.com',
    subject='DAG Notification',
    html_content='<p>Your DAG has completed.</p>'
)
email_task.execute(context={})
This code creates and runs an EmailOperator task that sends an email notification with a subject and HTML content.
Process Table
StepActionEvaluationResult
1Start EmailOperator executionN/AReady to send email
2Prepare email contentto='user@example.com', subject='DAG Notification', html_content='<p>Your DAG has completed.</p>'Email content ready
3Connect to SMTP serverSMTP server reachable?Connection established
4Send emailSend email via SMTPEmail sent successfully
5Confirm email sentEmail delivery statusSuccess confirmed
6End EmailOperator taskN/ATask completed
💡 Email sent successfully, task ends normally
Status Tracker
VariableStartAfter Step 2After Step 4Final
to'''user@example.com''user@example.com''user@example.com'
subject'''DAG Notification''DAG Notification''DAG Notification'
html_content'''<p>Your DAG has completed.</p>''<p>Your DAG has completed.</p>''<p>Your DAG has completed.</p>'
smtp_connectionNoneNoneConnectedClosed after send
email_statusNoneNoneSentConfirmed
Key Moments - 3 Insights
Why does the EmailOperator need SMTP server connection?
Because the EmailOperator sends emails by connecting to an SMTP server, as shown in step 3 of the execution_table. Without this connection, the email cannot be sent.
What happens if the SMTP server is not reachable?
If the SMTP server is unreachable, the EmailOperator will fail to send the email and log an error, as indicated by the 'No' branch in the concept_flow after 'Email sent successfully?'.
Why do we provide 'html_content' instead of plain text?
The EmailOperator supports HTML content to allow formatted emails. In the execution_table step 2, 'html_content' is prepared to send a nicely formatted message.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the email_status variable after step 4?
ANone
BSent
CConnected
DConfirmed
💡 Hint
Check variable_tracker column 'After Step 4' for 'email_status'
At which step does the EmailOperator confirm the email was sent successfully?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look at execution_table row with 'Confirm email sent'
If the SMTP server is unreachable, which part of the concept_flow will be triggered?
ATrigger EmailOperator
BPrepare email content
CLog error
DEnd EmailOperator task
💡 Hint
Refer to concept_flow decision after 'Email sent successfully?' with 'No' branch
Concept Snapshot
EmailOperator sends emails in Airflow DAGs.
Set 'to', 'subject', and 'html_content' to define the email.
It connects to SMTP server to send the email.
If SMTP fails, it logs an error.
Use execute() to run the task in code.
Full Transcript
The EmailOperator in Airflow sends email notifications during DAG runs. It starts by preparing the email content including recipient, subject, and HTML body. Then it connects to the SMTP server to send the email. If the connection and sending succeed, the task completes successfully. If the SMTP server is unreachable, the operator logs an error and fails. Variables like 'to', 'subject', and 'html_content' hold the email details, while 'smtp_connection' tracks the connection state and 'email_status' tracks sending status. This flow ensures notifications are sent reliably during workflows.