Coverage for jutil/management/commands/test_email.py: 22%

# Coverage for jutil/management/commands/test_email.py : 22%

## 36 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import os

2 from django.conf import settings

3 from django.core.management.base import CommandParser

4 from django.utils.timezone import now

5 from jutil.command import SafeCommand

6 from jutil.email import send_email, send_email_smtp, send_email_sendgrid

7

8

9 class Command(SafeCommand):

10 help = "Send test email with (optional) attachment"

11

12 def add_arguments(self, parser: CommandParser):

13 parser.add_argument("email", type=str)

14 parser.add_argument("--cc", type=str)

15 parser.add_argument("--bcc", type=str)

16 parser.add_argument("--sender", type=str)

17 parser.add_argument("--subject", type=str)

18 parser.add_argument("--body", type=str)

19 parser.add_argument("--attach", type=str, nargs="*")

20 parser.add_argument("--smtp", action="store_true")

21 parser.add_argument("--sendgrid", action="store_true")

22

23 def do(self, *args, **kw):

24 files = kw["attach"] if kw["attach"] else []

25 if not files:

26 files.append(os.path.join(settings.BASE_DIR, "data/attachment.jpg"))

27 subject = "hello " + now().isoformat()

28 text = "body text"

29 html = '<h1>html text</h1><p><a href="https://kajala.com/">Kajala Group Ltd.</a></p>'

30 if kw["body"]:

31 html = kw["body"]

32 if kw["subject"]:

33 subject = kw["subject"]

34 sender = kw["sender"] if kw["sender"] else ""

35

36 if kw["smtp"]:

37 res = send_email_smtp(

38 kw["email"],

39 subject,

40 text,

41 html,

42 sender,

43 files,

44 bcc_recipients=kw["bcc"],

45 cc_recipients=kw["cc"],

46 exceptions=True,

47 )

48 elif kw["sendgrid"]:

49 res = send_email_sendgrid(

50 kw["email"],

51 subject,

52 text,

53 html,

54 sender,

55 files,

56 bcc_recipients=kw["bcc"],

57 cc_recipients=kw["cc"],

58 exceptions=True,

59 )

60 else:

61 res = send_email(

62 kw["email"],

63 subject,

64 text,

65 html,

66 sender,

67 files,

68 bcc_recipients=kw["bcc"],

69 cc_recipients=kw["cc"],

70 exceptions=True,

71 )

72

73 print("send_email returned", res)