You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "henry3260 (via GitHub)" <gi...@apache.org> on 2026/02/10 00:58:29 UTC
[PR] Add SesEmailOperator [airflow]
henry3260 opened a new pull request, #61697:
URL: https://github.com/apache/airflow/pull/61697
<!--
Thank you for contributing!
Please provide above a brief description of the changes made in this pull request.
Write a good git commit message following this guide: http://chris.beams.io/posts/git-commit/
Please make sure that your code changes are covered with tests.
And in case of new features or big changes remember to adjust the documentation.
Feel free to ping (in general) for the review if you do not see reaction for a few days
(72 Hours is the minimum reaction time you can expect from volunteers) - we sometimes miss notifications.
In case of an existing issue, reference it using one of the following:
* closes: #ISSUE
* related: #ISSUE
-->
---
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR, please
change below checkbox to `[X]` followed by the name of the tool, uncomment the "Generated-by".
-->
- [ ] Yes (please specify the tool below)
<!--
Generated-by: [Tool Name] following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
-->
---
* Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
* For fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment: `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add SesEmailOperator [airflow]
Posted by "SameerMesiah97 (via GitHub)" <gi...@apache.org>.
SameerMesiah97 commented on code in PR #61697:
URL: https://github.com/apache/airflow/pull/61697#discussion_r2789958255
##########
providers/amazon/tests/system/amazon/aws/example_ses.py:
##########
@@ -0,0 +1,125 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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.
+from __future__ import annotations
+
+import os
+from datetime import datetime
+
+from airflow.providers.amazon.aws.operators.ses import SesEmailOperator
+
+from tests_common.test_utils.version_compat import AIRFLOW_V_3_0_PLUS
+
+if AIRFLOW_V_3_0_PLUS:
+ from airflow.sdk import DAG, chain, task
+else:
+ # Airflow 2 path
+ from airflow.decorators import task # type: ignore[attr-defined,no-redef]
+ from airflow.models.baseoperator import chain # type: ignore[attr-defined,no-redef]
+ from airflow.models.dag import DAG # type: ignore[attr-defined,no-redef,assignment]
+
+
+@task
+def get_verified_email() -> str:
+ email = os.getenv("SES_VERIFIED_EMAIL")
+ if not email:
+ raise ValueError(
+ "Please set SES_VERIFIED_EMAIL environment variable to a verified email address in your SES account"
+ )
+ return email
+
+
+with DAG(
+ dag_id="example_ses",
+ start_date=datetime(2021, 1, 1),
+ schedule="@once",
+ catchup=False,
+ tags=["example"],
+) as dag:
+ verified_email = get_verified_email()
+
+ # [START howto_operator_ses_email_basic]
+ # Basic email sending
+ # Note: In SES sandbox mode, both sender and recipient must be verified.
+ send_basic_email = SesEmailOperator(
+ task_id="send_basic_email",
+ mail_from=verified_email,
+ to=[verified_email],
+ subject="Test Email from Airflow",
+ html_content="<h1>Hello</h1><p>This is a test email sent via Amazon SES.</p>",
+ aws_conn_id="aws_default",
+ )
+ # [END howto_operator_ses_email_basic]
+
+ # [START howto_operator_ses_email_cc_bcc]
+ # Email with CC and BCC
+ send_email_with_cc_bcc = SesEmailOperator(
+ task_id="send_email_with_cc_bcc",
+ mail_from=verified_email,
+ to=[verified_email],
+ cc=[verified_email],
+ bcc=[verified_email],
+ subject="Test Email with CC and BCC",
+ html_content="<h1>Hello</h1><p>This email has CC and BCC recipients.</p>",
+ aws_conn_id="aws_default",
+ )
+ # [END howto_operator_ses_email_cc_bcc]
+
+ # [START howto_operator_ses_email_headers]
+ # Email with custom headers and reply-to
+ send_email_with_headers = SesEmailOperator(
+ task_id="send_email_with_headers",
+ mail_from=verified_email,
+ to=[verified_email],
+ subject="Test Email with Custom Headers",
+ html_content="<h1>Hello</h1><p>This email has custom headers.</p>",
+ reply_to=verified_email,
+ return_path=verified_email,
+ custom_headers={"X-Custom-Header": "CustomValue"},
+ aws_conn_id="aws_default",
+ )
+ # [END howto_operator_ses_email_headers]
+
+ # [START howto_operator_ses_email_templated]
+ # Email with template variables
+ send_templated_email = SesEmailOperator(
+ task_id="send_templated_email",
+ mail_from=verified_email,
+ to=[verified_email],
+ subject="DAG Run: {{ dag.dag_id }} - {{ ds }}",
+ html_content="""
+ <h1>DAG Run Report</h1>
+ <p>DAG ID: {{ dag.dag_id }}</p>
+ <p>Execution Date: {{ ds }}</p>
+ <p>Run ID: {{ run_id }}</p>
+ """,
+ aws_conn_id="aws_default",
+ )
+ # [END howto_operator_ses_email_templated]
+
+ chain(
+ verified_email,
+ send_basic_email,
+ send_email_with_cc_bcc,
+ send_email_with_headers,
+ send_templated_email,
+ )
Review Comment:
Non-blocking suggestion: As your implementation supports files, why not add another task like this:
```
send_email_with_attachment = SesEmailOperator(
task_id="send_email_with_attachment",
mail_from=verified_email,
to=[verified_email],
subject="Email with attachment",
html_content="<p>See attached file.</p>",
files=["/tmp/example.txt"],
aws_conn_id="aws_default",
)
```
##########
providers/amazon/src/airflow/providers/amazon/aws/operators/ses.py:
##########
@@ -0,0 +1,143 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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.
+"""Send email using Amazon Simple Email Service (SES)."""
+
+from __future__ import annotations
+
+from collections.abc import Iterable, Sequence
+from typing import TYPE_CHECKING, Any
+
+from airflow.providers.amazon.aws.hooks.ses import SesHook
+from airflow.providers.amazon.aws.operators.base_aws import AwsBaseOperator
+from airflow.providers.amazon.aws.utils.mixins import aws_template_fields
+
+if TYPE_CHECKING:
+ from airflow.sdk import Context
+
+
+class SesEmailOperator(AwsBaseOperator[SesHook]):
Review Comment:
This is more of a general point regarding this new operator. What happens if retries are set and there is a failure? Is this idempotent or can duplicate emails be sent? If that is the case, it would not hurt to mention this in the docstring. Not a blocking suggestion.
##########
providers/amazon/tests/unit/amazon/aws/operators/test_ses.py:
##########
@@ -0,0 +1,179 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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.
+from __future__ import annotations
+
+from unittest import mock
+
+import pytest
+
+from airflow.providers.amazon.aws.operators.ses import SesEmailOperator
+
+from unit.amazon.aws.utils.test_template_fields import validate_template_fields
+
+TASK_ID = "ses_email_job"
+AWS_CONN_ID = "custom_aws_conn"
+MAIL_FROM = "sender@example.com"
+TO = ["recipient@example.com"]
+SUBJECT = "Test Subject"
+HTML_CONTENT = "<h1>Test Email</h1><p>This is a test.</p>"
+
+
+class TestSesEmailOperator:
Review Comment:
With these tests you appeared to have covered all the happy path scenarios but what about when the operator fails to send an email for one reason or another. I would suggest one test to assert that the exception from the hook propagates to the operator.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add SesEmailOperator [airflow]
Posted by "o-nikolas (via GitHub)" <gi...@apache.org>.
o-nikolas commented on PR #61697:
URL: https://github.com/apache/airflow/pull/61697#issuecomment-3881050484
Why did you create a new PR for this work? All the previous context and reviewers are tagged on #58312
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add SesEmailOperator [airflow]
Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 commented on PR #61697:
URL: https://github.com/apache/airflow/pull/61697#issuecomment-3883046646
> Why did you create a new PR for this work? All the previous context and reviewers are tagged on #58312
Sorry for that because I
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add SesEmailOperator [airflow]
Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 closed pull request #61697: Add SesEmailOperator
URL: https://github.com/apache/airflow/pull/61697
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add SesEmailOperator [airflow]
Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 closed pull request #61697: Add SesEmailOperator
URL: https://github.com/apache/airflow/pull/61697
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add SesEmailOperator [airflow]
Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 commented on PR #61697:
URL: https://github.com/apache/airflow/pull/61697#issuecomment-3875931913
cc @eladkal, thought you might want to take a look since we discussed this earlier. :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org