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
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1"""Functions for processing emails."""
3from typing import Optional
5from django.core.mail.message import EmailMessage, EmailMultiAlternatives
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
14def process_mail_out(
15 email_messages: list[EmailMessage | EmailMultiAlternatives],
16) -> bool:
17 """Process email messages with plugins.
19 Args:
20 email_messages (list): List of EmailMessage objects to process.
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
29 # Ensure messages are a list
30 if not isinstance(email_messages, list):
31 email_messages = [email_messages]
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
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.
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.
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
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
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