Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example Airflow DAG that sends an email message through SendGrid #7894

Merged
47 changes: 47 additions & 0 deletions composer/workflows/email_operator_sendgrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Example Airflow DAG that sends an email message through SendGrid.
You must configure email notifications so that Airflow sends email through
SendGrid. For more information, see
https://cloud.google.com/composer/docs/composer-2/configure-email#sendgrid
"""

# [START composer_email_operator_sendgrid]

import datetime

import airflow
from airflow.operators.email import EmailOperator


with airflow.DAG(
"composer_sample_sendgrid",
start_date=datetime.datetime(2022, 1, 1),
) as dag:

task_email = EmailOperator(
task_id="send-email",
conn_id="sendgrid_default",
# You can specify more than one recipient with a list.
to="user@example.com",
subject="EmailOperator test for SendGrid",
html_content="This is a test message sent through SendGrid.",
dag=dag,
)

# [END composer_email_operator_sendgrid]
26 changes: 26 additions & 0 deletions composer/workflows/email_operator_sendgrid_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import internal_unit_testing


def test_email_operator_sendgrid() -> None:
"""Test that the DAG file can be successfully imported.
This tests that the DAG can be parsed, but does not run it in an Airflow
environment. This is a recommended confidence check by the official Airflow
docs: https://airflow.incubator.apache.org/tutorial.html#testing
"""
from . import email_operator_sendgrid

internal_unit_testing.assert_has_valid_dag(email_operator_sendgrid)