Apache Airflow SMTP Provider
The `apache-airflow-providers-smtp` package provides the SmtpHook, enabling Apache Airflow to send emails using SMTP. This is essential for features like email notifications in DAGs via the `EmailOperator`. The current version is 2.4.4, with releases typically synchronized with major Airflow releases or as needed for bug fixes and improvements.
Warnings
- gotcha The `EmailOperator` (and other email features) critically depend on a properly configured `smtp_default` connection in Airflow. This connection must be set up in the Airflow UI (Admin -> Connections) or in `airflow.cfg` with the correct host, port, authentication, and TLS/SSL settings. Without it, email tasks will fail with connection errors.
- gotcha This provider package requires Python >= 3.10. Installing it in environments with older Python versions will lead to installation errors or runtime incompatibilities.
- gotcha While the `EmailOperator` is part of Airflow core, its ability to send emails via SMTP relies entirely on the `apache-airflow-providers-smtp` package being installed. If the provider is missing, `EmailOperator` tasks will typically fail silently or with an error indicating a missing dependency or an inability to find a suitable hook.
Install
-
pip install apache-airflow-providers-smtp
Imports
- SmtpHook
from airflow.providers.smtp.hooks.smtp import SmtpHook
- EmailOperator
from airflow.operators.email import EmailOperator
Quickstart
from airflow.operators.email import EmailOperator
from airflow.models.dag import DAG
from datetime import datetime
# NOTE: For this DAG to send emails, an 'smtp_default' connection
# must be configured in Airflow (Admin -> Connections) or via airflow.cfg.
# The apache-airflow-providers-smtp package provides the SmtpHook that EmailOperator uses.
with DAG(
dag_id='simple_email_notification',
start_date=datetime(2023, 1, 1),
schedule_interval=None,
catchup=False,
tags=['email_example'],
) as dag:
send_test_email = EmailOperator(
task_id='send_test_email',
to='your_recipient@example.com', # Replace with a valid email address
subject='Airflow Test Email from SMTP Provider',
html_content='<h3>Hello from Airflow!</h3><p>This email confirms your SMTP provider is working.</p>',
conn_id='smtp_default', # This connection ID should be configured
)