How to Configure SMTP in Airflow for Email Notifications
To configure
SMTP in Airflow, set the SMTP server details in the airflow.cfg file under the [smtp] section. Provide your SMTP host, port, user, password, and sender email to enable Airflow to send emails for alerts and notifications.Syntax
The SMTP configuration in Airflow is done in the airflow.cfg file under the [smtp] section. Key settings include:
smtp_host: The address of your SMTP server.smtp_starttls: Whether to use STARTTLS encryption (True/False).smtp_ssl: Whether to use SSL encryption (True/False).smtp_user: Your SMTP username (email or login).smtp_password: Your SMTP password.smtp_port: The port number your SMTP server listens on (usually 587 for TLS or 465 for SSL).smtp_mail_from: The email address that appears as the sender.
ini
[smtp]
smtp_host = smtp.example.com
smtp_starttls = True
smtp_ssl = False
smtp_user = your_email@example.com
smtp_password = your_password
smtp_port = 587
smtp_mail_from = your_email@example.comExample
This example shows a complete SMTP configuration in airflow.cfg to send emails using Gmail's SMTP server with STARTTLS enabled.
ini
[smtp]
smtp_host = smtp.gmail.com
smtp_starttls = True
smtp_ssl = False
smtp_user = your_gmail_address@gmail.com
smtp_password = your_app_password
smtp_port = 587
smtp_mail_from = your_gmail_address@gmail.comOutput
No direct output; Airflow will send emails for alerts using this SMTP setup.
Common Pitfalls
Common mistakes when configuring SMTP in Airflow include:
- Using incorrect SMTP port or host.
- Not enabling STARTTLS or SSL when required by the SMTP server.
- Using the wrong username or password, especially if two-factor authentication is enabled (use app passwords).
- Not setting
smtp_mail_from, causing emails to fail. - Forgetting to restart the Airflow scheduler and webserver after changing
airflow.cfg.
Always test your SMTP settings by triggering an email alert in Airflow.
Quick Reference
| Setting | Description | Typical Value |
|---|---|---|
| smtp_host | SMTP server address | smtp.gmail.com |
| smtp_starttls | Enable STARTTLS encryption | True |
| smtp_ssl | Enable SSL encryption | False |
| smtp_user | SMTP username/email | your_email@example.com |
| smtp_password | SMTP password or app password | your_password |
| smtp_port | SMTP server port | 587 |
| smtp_mail_from | Sender email address | your_email@example.com |
Key Takeaways
Configure SMTP settings in the [smtp] section of airflow.cfg to enable email notifications.
Use correct SMTP host, port, and encryption settings matching your email provider.
Use app passwords if your email provider requires two-factor authentication.
Set smtp_mail_from to a valid sender email to avoid email delivery issues.
Restart Airflow services after changing SMTP configuration to apply changes.