Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions airflow/providers/amazon/aws/utils/emailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
# specific language governing permissions and limitations
# under the License.
"""Airflow module for email backend using AWS SES"""

from typing import List, Optional, Union

from airflow.providers.amazon.aws.hooks.ses import SesHook


def send_email(
from_email: str,
to: Union[List[str], str],
subject: str,
html_content: str,
Expand All @@ -33,9 +31,12 @@ def send_email(
mime_subtype: str = 'mixed',
mime_charset: str = 'utf-8',
conn_id: str = 'aws_default',
from_email: Optional[str] = None,
**kwargs,
) -> None:
"""Email backend for SES."""
if from_email is None:
raise RuntimeError("The `from_email' configuration has to be set for the SES emailer.")
hook = SesHook(aws_conn_id=conn_id)
hook.send_email(
mail_from=from_email,
Expand Down
5 changes: 3 additions & 2 deletions docs/apache-airflow/howto/email-config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ Follow the steps below to enable it:
[email]
email_backend = airflow.providers.amazon.aws.utils.emailer.send_email
email_conn_id = aws_default
from_email = From email <email@example.com>

Note that for SES, you must configure from_email to the valid email that can send messages from SES.

3. Create a connection called ``aws_default``, or choose a custom connection
name and set it in ``email_conn_id``. The type of connection should be ``Amazon Web Services``.

4. Configure sender's email address in your ``airflow.cfg`` by setting ``from_email`` in the ``[email]`` section.
11 changes: 10 additions & 1 deletion tests/providers/amazon/aws/utils/test_emailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
#
from unittest import TestCase, mock

import pytest

from airflow.providers.amazon.aws.utils.emailer import send_email


class TestSendEmailSes(TestCase):
@mock.patch("airflow.providers.amazon.aws.utils.emailer.SesHook")
def test_send_ses_email(self, mock_hook):
send_email(
from_email="From Test <from@test.com>",
to="to@test.com",
subject="subject",
html_content="content",
from_email="From Test <from@test.com>",
)

mock_hook.return_value.send_email.assert_called_once_with(
Expand All @@ -42,3 +44,10 @@ def test_send_ses_email(self, mock_hook):
mime_charset="utf-8",
mime_subtype="mixed",
)

@mock.patch("airflow.providers.amazon.aws.utils.emailer.SesHook")
def test_send_ses_email_no_from_mail(self, mock_hook):
with pytest.raises(
RuntimeError, match="The `from_email' configuration has to be set for the SES emailer."
):
send_email(to="to@test.com", subject="subject", html_content="content")