Send email using SendGrid¶
Airflow can be configured to send e-mail using SendGrid, either through its SMTP relay or through this provider’s API-based backend. See Email Configuration for the general Airflow email configuration this page builds on.
Using Default SMTP¶
You can use the default Airflow SMTP backend to send email with SendGrid without installing this provider:
[smtp]
smtp_host=smtp.sendgrid.net
smtp_starttls=False
smtp_ssl=False
smtp_port=587
smtp_mail_from=<your-from-email>
Equivalent environment variables look like:
AIRFLOW__SMTP__SMTP_HOST=smtp.sendgrid.net
AIRFLOW__SMTP__SMTP_STARTTLS=False
AIRFLOW__SMTP__SMTP_SSL=False
AIRFLOW__SMTP__SMTP_PORT=587
AIRFLOW__SMTP__SMTP_MAIL_FROM=<your-from-email>
Using the SendGrid Provider¶
To send email through SendGrid’s API instead, follow the steps below:
Set up your SendGrid account, then locate your SMTP username and API Key.
Install the
sendgridprovider as part of your Airflow installation, e.g.:pip install 'apache-airflow[sendgrid]' --constraint ...
or
pip install 'apache-airflow-providers-sendgrid' --constraint ...
Update the
[email]section inairflow.cfg:[email] email_backend = airflow.providers.sendgrid.utils.emailer.send_email email_conn_id = sendgrid_default from_email = "hello@eg.com"
Equivalent environment variables look like:
AIRFLOW__EMAIL__EMAIL_BACKEND=airflow.providers.sendgrid.utils.emailer.send_email AIRFLOW__EMAIL__EMAIL_CONN_ID=sendgrid_default SENDGRID_MAIL_FROM=hello@thelearning.dev
Create a connection called
sendgrid_default, or choose a custom connection name and set it inemail_conn_id, ofEmailtype. Only the login and password fields are used from the connection; set the password field to your SendGrid API Key. See Managing Connections for how to create a connection.
Note
The callbacks for success, failure and retry will use the same configuration to send the email.