Apache Airflow SendGrid Provider

4.2.2 · active · verified Wed Apr 15

The `apache-airflow-providers-sendgrid` package integrates Apache Airflow with SendGrid, a popular email delivery service, allowing users to send emails programmatically from Airflow DAGs. It includes operators and hooks to interact with the SendGrid API. The provider is actively maintained, with version 4.2.2 being the latest, and releases frequently in alignment with Apache Airflow's provider release cycle.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart DAG demonstrates how to use the `SendGridOperator` to send an email. It assumes a SendGrid connection named `sendgrid_default` is configured in Airflow. For local testing, you can set the connection via an environment variable `AIRFLOW_CONN_SENDGRID_DEFAULT` using your SendGrid API Key.

import os
from datetime import datetime

from airflow import DAG
from airflow.providers.sendgrid.operators.sendgrid import SendGridOperator

# Ensure you have a SendGrid API Key configured in Airflow Connections
# named 'sendgrid_default'. For a quick start, you can set an environment variable:
# export AIRFLOW_CONN_SENDGRID_DEFAULT='sendgrid://:YOUR_SENDGRID_API_KEY@'
# Replace 'YOUR_SENDGRID_API_KEY' with your actual key (starting with SG.)

with DAG(
    dag_id='sendgrid_email_example',
    start_date=datetime(2023, 1, 1),
    schedule_interval=None,
    catchup=False,
    tags=['sendgrid', 'email'],
) as dag:
    send_email_task = SendGridOperator(
        task_id='send_test_email',
        sendgrid_conn_id='sendgrid_default',
        to='recipient@example.com', # Replace with actual recipient
        subject='Airflow SendGrid Test Email',
        html_content='<strong>This is a test email from Airflow via SendGrid!</strong>',
        from_email='sender@example.com', # Replace with actual sender (verified in SendGrid)
    )

view raw JSON →