Coverage for src/backend/InvenTree/plugin/base/mail/mail.py: 0%

37 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Functions for processing emails.""" 

2 

3from typing import Optional 

4 

5from django.core.mail.message import EmailMessage, EmailMultiAlternatives 

6 

7from common.models import EmailMessage as InventreeEmailMessage 

8from common.settings import get_global_setting 

9from InvenTree.backends import logger 

10from plugin.plugin import PluginMixinEnum 

11from plugin.registry import registry 

12 

13 

14def process_mail_out( 

15 email_messages: list[EmailMessage | EmailMultiAlternatives], 

16) -> bool: 

17 """Process email messages with plugins. 

18 

19 Args: 

20 email_messages (list): List of EmailMessage objects to process. 

21 

22 Returns: 

23 bool: True if processing was successful, False otherwise. 

24 """ 

25 if not get_global_setting('ENABLE_PLUGINS_MAILS', False): 

26 # Do nothing if plugin mails are not enabled 

27 return False 

28 

29 # Ensure messages are a list 

30 if not isinstance(email_messages, list): 

31 email_messages = [email_messages] 

32 

33 for plugin in registry.with_mixin(PluginMixinEnum.MAIL): 

34 for message in email_messages: 

35 try: 

36 plugin.process_mail_out(message) 

37 except Exception: # pragma: no cover 

38 logger.exception( 

39 'Exception during mail processing for plugin %s', plugin.slug 

40 ) 

41 return True 

42 

43 

44def process_mail_in( 

45 email_message: Optional[InventreeEmailMessage] = None, mail_id: Optional[int] = None 

46) -> bool: 

47 """Process an incoming email message with plugins. 

48 

49 Args: 

50 email_message (Optional[InventreeEmailMessage]): The email message to process. 

51 mail_id (int): The ID of the email message to process if email_message is None. This is required if running through the tasks framework. 

52 

53 Returns: 

54 bool: True if processing was successful, False otherwise. 

55 """ 

56 if not get_global_setting('ENABLE_PLUGINS_MAILS', False): 

57 # Do nothing if plugin mails are not enabled 

58 return False 

59 

60 if email_message is None: 

61 if mail_id is None: 

62 logger.error('No email message or mail ID provided for processing') 

63 return False 

64 try: 

65 email_message = InventreeEmailMessage.objects.get(id=mail_id) 

66 except InventreeEmailMessage.DoesNotExist: 

67 logger.error('Email message with ID %s does not exist', mail_id) 

68 return False 

69 

70 for plugin in registry.with_mixin(PluginMixinEnum.MAIL): 

71 try: 

72 plugin.process_mail_in(email_message) 

73 except Exception: # pragma: no cover 

74 logger.exception( 

75 'Exception during mail processing for plugin %s', plugin.slug 

76 ) 

77 return True