Repository: wagtail/wagtail

Issue:
kwargs lost in send_email?
Dear all, 

the wagtail.admin.mail ``send_mail``  method takes keyword arguments [1] but does not pass them to the wrapped Django's ``EmailMultiAlternatives``. 

So, for example, the ``bcc`` or ``reply_to`` in a custom ``AbstractEmailForm``
```
send_mail(
    subject, content,
    to_addresses,
    from_address,
    bcc=bcc,
    reply_to=reply_to,
)
```
did not get passed to the wrapped ``EmailMultiAlternatives`` [3]

[1] https://github.com/wagtail/wagtail/blob/36e777418f308fd6aa359e1c953afcf4ab041571/wagtail/admin/mail.py#L17
[2] https://github.com/wagtail/wagtail/blob/98c2aefbe801b68346b4c345c44f840d60c911e8/wagtail/contrib/forms/models.py#L261
[3] https://github.com/django/django/blob/53b6a466d89f972aa435fe7b5135e9b501ce68ee/django/core/mail/message.py#L400


Discussion:

Resolving patch:
diff --git a/wagtail/admin/mail.py b/wagtail/admin/mail.py
--- a/wagtail/admin/mail.py
+++ b/wagtail/admin/mail.py
@@ -54,6 +54,9 @@ def send_mail(subject, message, recipient_list, from_email=None, **kwargs):
         "headers": {
             "Auto-Submitted": "auto-generated",
         },
+        "bcc": kwargs.get("bcc", None),
+        "cc": kwargs.get("cc", None),
+        "reply_to": kwargs.get("reply_to", None),
     }
     mail = EmailMultiAlternatives(
         subject, message, from_email, recipient_list, **multi_alt_kwargs


Test patch:
diff --git a/wagtail/admin/tests/tests.py b/wagtail/admin/tests/tests.py
--- a/wagtail/admin/tests/tests.py
+++ b/wagtail/admin/tests/tests.py
@@ -235,6 +235,78 @@ def test_send_html_email(self):
         self.assertEqual(email_message.body, "TEXT content")
         self.assertEqual(email_message.to, ["mr.plain.text@email.com"])
 
+    def test_send_cc(self):
+        send_mail(
+            "Test subject",
+            "Test content",
+            ["nobody@email.com"],
+            "test@email.com",
+            cc=["cc.test@email.com"],
+        )
+
+        # Check that the email was sent
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(mail.outbox[0].subject, "Test subject")
+        self.assertEqual(mail.outbox[0].body, "Test content")
+        self.assertEqual(mail.outbox[0].to, ["nobody@email.com"])
+        self.assertEqual(mail.outbox[0].from_email, "test@email.com")
+        self.assertEqual(mail.outbox[0].cc, ["cc.test@email.com"])
+
+    def test_send_bcc(self):
+        send_mail(
+            "Test subject",
+            "Test content",
+            ["nobody@email.com"],
+            "test@email.com",
+            bcc=["bcc.test@email.com"],
+        )
+
+        # Check that the email was sent
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(mail.outbox[0].subject, "Test subject")
+        self.assertEqual(mail.outbox[0].body, "Test content")
+        self.assertEqual(mail.outbox[0].to, ["nobody@email.com"])
+        self.assertEqual(mail.outbox[0].from_email, "test@email.com")
+        self.assertEqual(mail.outbox[0].bcc, ["bcc.test@email.com"])
+
+    def test_send_reply_to(self):
+        send_mail(
+            "Test subject",
+            "Test content",
+            ["nobody@email.com"],
+            "test@email.com",
+            reply_to=["reply_to.test@email.com"],
+        )
+
+        # Check that the email was sent
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(mail.outbox[0].subject, "Test subject")
+        self.assertEqual(mail.outbox[0].body, "Test content")
+        self.assertEqual(mail.outbox[0].to, ["nobody@email.com"])
+        self.assertEqual(mail.outbox[0].from_email, "test@email.com")
+        self.assertEqual(mail.outbox[0].reply_to, ["reply_to.test@email.com"])
+
+    def test_send_all_extra_fields(self):
+        send_mail(
+            "Test subject",
+            "Test content",
+            ["nobody@email.com"],
+            "test@email.com",
+            cc=["cc.test@email.com"],
+            bcc=["bcc.test@email.com"],
+            reply_to=["reply_to.test@email.com"],
+        )
+
+        # Check that the email was sent
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(mail.outbox[0].subject, "Test subject")
+        self.assertEqual(mail.outbox[0].body, "Test content")
+        self.assertEqual(mail.outbox[0].to, ["nobody@email.com"])
+        self.assertEqual(mail.outbox[0].from_email, "test@email.com")
+        self.assertEqual(mail.outbox[0].cc, ["cc.test@email.com"])
+        self.assertEqual(mail.outbox[0].bcc, ["bcc.test@email.com"])
+        self.assertEqual(mail.outbox[0].reply_to, ["reply_to.test@email.com"])
+
 
 class TestTagsAutocomplete(TestCase, WagtailTestUtils):
     def setUp(self):