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(kw['email'], subject, text, html, sender, files, bcc_recipients=kw['bcc'], cc_recipients=kw['cc'], exceptions=True)

38 elif kw['sendgrid']:

39 res = send_email_sendgrid(kw['email'], subject, text, html, sender, files, bcc_recipients=kw['bcc'], cc_recipients=kw['cc'], exceptions=True)

40 else:

41 res = send_email(kw['email'], subject, text, html, sender, files, bcc_recipients=kw['bcc'], cc_recipients=kw['cc'], exceptions=True)

42

43 print('send_email returned', res)