Skip to content

Commit 0666a6a

Browse files
Example Airflow DAG that sends an email message through SendGrid (#7894)
* Example Airflow DAG that sends an email message through SendGrid * Fix region tag format * Fix Copyright header check * Remove daily scheduling and unneeded task name * Lint the sample * Add unit test * Fix imports in the unit test * Update composer/workflows/email_operator_sendgrid_test.py Co-authored-by: Leah E. Cole <[email protected]>
1 parent 4762700 commit 0666a6a

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
"""Example Airflow DAG that sends an email message through SendGrid.
17+
18+
You must configure email notifications so that Airflow sends email through
19+
SendGrid. For more information, see
20+
https://cloud.google.com/composer/docs/composer-2/configure-email#sendgrid
21+
22+
"""
23+
24+
# [START composer_email_operator_sendgrid]
25+
26+
import datetime
27+
28+
import airflow
29+
from airflow.operators.email import EmailOperator
30+
31+
32+
with airflow.DAG(
33+
"composer_sample_sendgrid",
34+
start_date=datetime.datetime(2022, 1, 1),
35+
) as dag:
36+
37+
task_email = EmailOperator(
38+
task_id="send-email",
39+
conn_id="sendgrid_default",
40+
# You can specify more than one recipient with a list.
41+
42+
subject="EmailOperator test for SendGrid",
43+
html_content="This is a test message sent through SendGrid.",
44+
dag=dag,
45+
)
46+
47+
# [END composer_email_operator_sendgrid]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import internal_unit_testing
16+
17+
18+
def test_email_operator_sendgrid() -> None:
19+
"""Test that the DAG file can be successfully imported.
20+
This tests that the DAG can be parsed, but does not run it in an Airflow
21+
environment. This is a recommended confidence check by the official Airflow
22+
docs: https://airflow.incubator.apache.org/tutorial.html#testing
23+
"""
24+
from . import email_operator_sendgrid
25+
26+
internal_unit_testing.assert_has_valid_dag(email_operator_sendgrid)

0 commit comments

Comments
 (0)