Skip to content

Commit

Permalink
feat: make cc and bcc templated fields in EmailOperator (#35235)
Browse files Browse the repository at this point in the history
  • Loading branch information
joffreybienvenu-infrabel committed Oct 28, 2023
1 parent 8ea094f commit 7857ca2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 3 additions & 3 deletions airflow/providers/smtp/operators/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class EmailOperator(BaseOperator):
:param html_content: content of the email, html markup
is allowed. (templated)
:param files: file names to attach in email (templated)
:param cc: list of recipients to be added in CC field
:param bcc: list of recipients to be added in BCC field
:param cc: list of recipients to be added in CC field (templated)
:param bcc: list of recipients to be added in BCC field (templated)
:param mime_subtype: MIME sub content type
:param mime_charset: character set parameter added to the Content-Type
header.
:param custom_headers: additional headers to add to the MIME message.
"""

template_fields: Sequence[str] = ("to", "from_email", "subject", "html_content", "files")
template_fields: Sequence[str] = ("to", "from_email", "subject", "html_content", "files", "cc", "bcc")
template_fields_renderers = {"html_content": "html"}
template_ext: Sequence[str] = (".html",)
ui_color = "#e6faf9"
Expand Down
11 changes: 10 additions & 1 deletion tests/providers/smtp/operators/test_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@


class TestEmailOperator:
def setup_method(self):
self.default_op_kwargs = dict(to="to", subject="subject", html_content="content")

@patch("airflow.providers.smtp.hooks.smtp.SmtpHook.get_connection")
@patch(smtplib_string)
def test_loading_sender_email_from_connection(self, mock_smtplib, mock_hook_conn):
Expand All @@ -46,7 +49,13 @@ def test_loading_sender_email_from_connection(self, mock_smtplib, mock_hook_conn
),
)
smtp_client_mock = mock_smtplib.SMTP_SSL()
op = EmailOperator(task_id="test_email", to="to", subject="subject", html_content="content")
op = EmailOperator(task_id="test_email", **self.default_op_kwargs)
op.execute({})
call_args = smtp_client_mock.sendmail.call_args.kwargs
assert call_args["from_addr"] == sender_email

def test_assert_templated_fields(self):
"""Test expected templated fields."""
operator = EmailOperator(task_id="test_assert_templated_fields", **self.default_op_kwargs)
template_fields = ("to", "from_email", "subject", "html_content", "files", "cc", "bcc")
assert operator.template_fields == template_fields

0 comments on commit 7857ca2

Please sign in to comment.