2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-26 18:46:44 +00:00

Fix for email sending (#9526)

* Fix for email sending

- Extract valid email for user
- Do not send if email not configured for user

* Improve email address filtering logic

* Fix return type hint
This commit is contained in:
Oliver 2025-04-17 17:29:26 +10:00 committed by GitHub
parent fd4caceed0
commit afbcfe66bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 4 deletions

View File

@ -24,8 +24,6 @@ services:
volumes:
- ../:/home/inventree:z
- /tmp/.X11-unix:/tmp/.X11-unix
ports:
- 8000
environment:
INVENTREE_DB_ENGINE: postgresql

View File

@ -4,6 +4,7 @@ from django.conf import settings
from django.core import mail as django_mail
import structlog
from allauth.account.models import EmailAddress
import InvenTree.ready
import InvenTree.tasks
@ -56,7 +57,6 @@ def send_email(subject, body, recipients, from_email=None, html_message=None):
recipients = [recipients]
import InvenTree.ready
import InvenTree.status
if InvenTree.ready.isImportingData():
# If we are importing data, don't send emails
@ -89,3 +89,19 @@ def send_email(subject, body, recipients, from_email=None, html_message=None):
html_message=html_message,
group='notification',
)
def get_email_for_user(user) -> str:
"""Find an email address for the specified user."""
# First check if the user has an associated email address
if user.email:
return user.email
# Otherwise, find first matching email
# Priority is given to primary or verified email addresses
if (
email := EmailAddress.objects.filter(user=user)
.order_by('-primary', '-verified')
.first()
):
return email.email

View File

@ -705,4 +705,7 @@ def email_user(user_id: int, subject: str, message: str) -> None:
logger.warning('User <%s> not found - cannot send welcome message', user_id)
return
user.email_user(subject=subject, message=message)
from InvenTree.helpers_email import get_email_for_user, send_email
if email := get_email_for_user(user):
send_email(subject, message, [email])