diff --git a/src/backend/InvenTree/InvenTree/serializers.py b/src/backend/InvenTree/InvenTree/serializers.py index 30599b8d0aa1..2a5f730620fd 100644 --- a/src/backend/InvenTree/InvenTree/serializers.py +++ b/src/backend/InvenTree/InvenTree/serializers.py @@ -510,6 +510,7 @@ def validate(self, attrs): def create(self, validated_data): """Send an e email to the user after creation.""" from InvenTree.helpers_model import get_base_url + from InvenTree.tasks import email_user, offload_task base_url = get_base_url() @@ -527,8 +528,12 @@ def create(self, validated_data): if base_url: message += f'\n\nURL: {base_url}' + subject = _('Welcome to InvenTree') + # Send the user an onboarding email (from current site) - instance.email_user(subject=_('Welcome to InvenTree'), message=message) + offload_task( + email_user, instance.pk, str(subject), str(message), force_async=True + ) return instance diff --git a/src/backend/InvenTree/InvenTree/tasks.py b/src/backend/InvenTree/InvenTree/tasks.py index 85e89d64166c..37f8cb2747ff 100644 --- a/src/backend/InvenTree/InvenTree/tasks.py +++ b/src/backend/InvenTree/InvenTree/tasks.py @@ -12,6 +12,7 @@ from typing import Callable, Optional from django.conf import settings +from django.contrib.auth import get_user_model from django.core.exceptions import AppRegistryNotReady from django.core.management import call_command from django.db import DEFAULT_DB_ALIAS, connections @@ -693,3 +694,14 @@ def set_pending_migrations(n: int): # We should be current now - triggering full reload to make sure all models # are loaded fully in their new state. registry.reload_plugins(full_reload=True, force_reload=True, collect=True) + + +def email_user(user_id: int, subject: str, message: str) -> None: + """Send a message to a user.""" + try: + user = get_user_model().objects.get(pk=user_id) + except Exception: + logger.warning('User <%s> not found - cannot send welcome message', user_id) + return + + user.email_user(subject=subject, message=message)