How to Configure Email in Airflow for Notifications
To configure email in Airflow, set SMTP server details in the
airflow.cfg file under the [smtp] section. Then use Airflow's EmailOperator or configure alert emails in DAGs for notifications.Syntax
Airflow email configuration requires setting SMTP parameters in the airflow.cfg file. Key parameters include:
smtp_host: Your SMTP server address.smtp_starttls: Enable TLS encryption (true/false).smtp_ssl: Enable SSL encryption (true/false).smtp_user: SMTP username (email address).smtp_password: SMTP password.smtp_port: SMTP server port (usually 587 for TLS or 465 for SSL).smtp_mail_from: The sender email address shown in emails.
After configuring SMTP, use the EmailOperator in DAGs to send emails.
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 how to send an email using Airflow's EmailOperator in a DAG. It sends a simple email when the task runs.
python
from airflow import DAG from airflow.operators.email import EmailOperator from datetime import datetime default_args = { 'start_date': datetime(2024, 1, 1), } dag = DAG('email_example', default_args=default_args, schedule_interval='@once') send_email = EmailOperator( task_id='send_email', to='recipient@example.com', subject='Airflow Email Test', html_content='<h3>This is a test email from Airflow</h3>', dag=dag )
Output
Task send_email sends an email to recipient@example.com with subject 'Airflow Email Test'.
Common Pitfalls
- Incorrect SMTP settings: Using wrong host, port, or credentials causes email failures.
- Missing TLS/SSL: Not enabling
smtp_starttlsorsmtp_sslwhen required leads to connection errors. - Firewall or network blocks: SMTP ports might be blocked by firewalls.
- Not restarting Airflow: Changes in
airflow.cfgrequire restarting Airflow services. - Using plain text passwords: Avoid storing passwords in plain text; consider environment variables or secrets backend.
ini
### Wrong SMTP port example (causes failure): [smtp] smtp_port = 25 ### Correct SMTP port example: [smtp] smtp_port = 587
Quick Reference
Summary tips for configuring email in Airflow:
- Set SMTP details correctly in
airflow.cfgunder[smtp]. - Use
EmailOperatorto send emails in DAGs. - Restart Airflow after config changes.
- Test email sending with a simple DAG before production use.
- Secure SMTP credentials using environment variables or Airflow connections.
Key Takeaways
Configure SMTP settings in airflow.cfg under the [smtp] section to enable email sending.
Use EmailOperator in DAGs to send emails for notifications or alerts.
Always restart Airflow services after changing email configuration.
Verify SMTP credentials and ports to avoid connection errors.
Secure sensitive SMTP passwords using environment variables or Airflow's secrets backend.