0
0
AirflowHow-ToBeginner ยท 3 min read

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.com
๐Ÿ’ป

Example

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.com
Output
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

SettingDescriptionTypical Value
smtp_hostSMTP server addresssmtp.gmail.com
smtp_starttlsEnable STARTTLS encryptionTrue
smtp_sslEnable SSL encryptionFalse
smtp_userSMTP username/emailyour_email@example.com
smtp_passwordSMTP password or app passwordyour_password
smtp_portSMTP server port587
smtp_mail_fromSender email addressyour_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.