• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

inventree / InvenTree / 8604849533

08 Apr 2024 06:20PM CUT coverage: 78.857%. First build
8604849533

Pull #6981

github

web-flow
Merge 3d6fa4389 into 4adce85ef
Pull Request #6981: Bump djangorestframework from 3.14.0 to 3.15.1 in /src/backend

19152 of 24287 relevant lines covered (78.86%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

5.0
/src/backend/InvenTree/InvenTree/email.py
1
"""Code for managing email functionality in InvenTree."""
2

3
import logging
4

5
from django.conf import settings
6
from django.core import mail as django_mail
7

8
import InvenTree.ready
9
import InvenTree.tasks
10

11
logger = logging.getLogger('inventree')
12

13

14
def is_email_configured():
15
    """Check if email backend is configured.
16

17
    NOTE: This does not check if the configuration is valid!
18
    """
19
    configured = True
×
20
    testing = settings.TESTING
×
21

22
    if InvenTree.ready.isInTestMode():
×
23
        return False
×
24

25
    if InvenTree.ready.isImportingData():
×
26
        return False
×
27

28
    if not settings.EMAIL_HOST:
×
29
        configured = False
×
30

31
        # Display warning unless in test mode
32
        if not testing:  # pragma: no cover
×
33
            logger.debug('EMAIL_HOST is not configured')
×
34

35
    # Display warning unless in test mode
36
    if not settings.EMAIL_HOST_USER and not testing:  # pragma: no cover
×
37
        logger.debug('EMAIL_HOST_USER is not configured')
×
38

39
    # Display warning unless in test mode
40
    if not settings.EMAIL_HOST_PASSWORD and testing:  # pragma: no cover
×
41
        logger.debug('EMAIL_HOST_PASSWORD is not configured')
×
42

43
    # Email sender must be configured
44
    if not settings.DEFAULT_FROM_EMAIL:
×
45
        configured = False
×
46

47
        if not testing:  # pragma: no cover
×
48
            logger.debug('DEFAULT_FROM_EMAIL is not configured')
×
49

50
    return configured
×
51

52

53
def send_email(subject, body, recipients, from_email=None, html_message=None):
1✔
54
    """Send an email with the specified subject and body, to the specified recipients list."""
55
    if isinstance(recipients, str):
56
        recipients = [recipients]
57

58
    import InvenTree.ready
59
    import InvenTree.status
60

61
    if InvenTree.ready.isImportingData():
62
        # If we are importing data, don't send emails
63
        return
64

65
    if not InvenTree.email.is_email_configured() and not settings.TESTING:
66
        # Email is not configured / enabled
67
        return
68

69
    # If a *from_email* is not specified, ensure that the default is set
70
    if not from_email:
71
        from_email = settings.DEFAULT_FROM_EMAIL
72

73
        # If we still don't have a valid from_email, then we can't send emails
74
        if not from_email:
75
            if settings.TESTING:
76
                from_email = 'from@test.com'
77
            else:
78
                logger.error('send_email failed: DEFAULT_FROM_EMAIL not specified')
79
                return
80

81
    InvenTree.tasks.offload_task(
82
        django_mail.send_mail,
83
        subject,
84
        body,
85
        from_email,
86
        recipients,
87
        fail_silently=False,
88
        html_message=html_message,
89
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc