Skip to content

Commit

Permalink
Merge pull request #13 from systemallica/master
Browse files Browse the repository at this point in the history
fix: take into account cc, bcc and reply_to in async emails + fix typing
  • Loading branch information
marcosgabarda committed Mar 2, 2021
2 parents 4735ab1 + c46f71f commit 953e0cd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
21 changes: 14 additions & 7 deletions snitch/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class TemplateEmailMessage:
default_template_name: str = ""
default_subject: str = ""
default_from_email: str = ""
default_reply_to: str = ""
default_bcc: str = ""
default_cc: str = ""
default_reply_to: List[str] = [""]
default_bcc: List[str] = [""]
default_cc: List[str] = [""]
fake: bool = False
use_i18n: bool = False

Expand All @@ -34,9 +34,9 @@ def __init__(
from_email: Optional[str] = None,
attaches: Optional[List] = None,
template_name: Optional[str] = None,
reply_to: Optional[str] = None,
bcc: Optional[str] = None,
cc: Optional[str] = None,
reply_to: Optional[List] = None,
bcc: Optional[List] = None,
cc: Optional[List] = None,
):
self.template_name = (
self.default_template_name if template_name is None else template_name
Expand Down Expand Up @@ -75,7 +75,14 @@ def preview(self) -> str:
def async_send(self, message, message_txt):
if not self.fake:
send_email_asynchronously.delay(
self.subject, message_txt, message, self.from_email, self.to
self.subject,
message_txt,
message,
self.from_email,
self.to,
self.cc,
self.bcc,
self.reply_to,
)
if self.attaches:
warnings.warn(
Expand Down
17 changes: 15 additions & 2 deletions snitch/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@ def send_notification_task(notification_pk: int) -> Optional[bool]:

@task(serializer="json")
def send_email_asynchronously(
subject: str, message_txt: str, message: str, from_email: str, to: Union[List, str]
subject: str,
message_txt: str,
message: str,
from_email: str,
to: Union[List, str],
cc: Optional[List],
bcc: Optional[List],
reply_to: Optional[List],
):
"""Sends an email as a asynchronous task."""
email = EmailMultiAlternatives(
subject=subject, body=message_txt, from_email=from_email, to=to
subject=subject,
body=message_txt,
from_email=from_email,
to=to,
cc=cc,
bcc=bcc,
reply_to=reply_to,
)
email.attach_alternative(message, "text/html")
email.send()
Expand Down
17 changes: 16 additions & 1 deletion tests/test_snitch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import locale
import warnings

from django.test import TestCase
Expand Down Expand Up @@ -177,3 +176,19 @@ def test_send_email(self):
except Exception:
exception = True
self.assertFalse(exception)

def test_send_email_with_cc_and_bcc(self):
try:
email = WelcomeEmail(
to="test@example.com",
cc=["test@test.com"],
bcc=["test@tost.com"],
context={},
)
email.send(use_async=False)
self.assertEqual(email.cc, ["test@test.com"])
self.assertEqual(email.bcc, ["test@tost.com"])
exception = False
except Exception:
exception = True
self.assertFalse(exception)

0 comments on commit 953e0cd

Please sign in to comment.