Skip to content

Commit afbcfe6

Browse files
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
1 parent fd4cace commit afbcfe6

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

.devcontainer/docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ services:
2424
volumes:
2525
- ../:/home/inventree:z
2626
- /tmp/.X11-unix:/tmp/.X11-unix
27-
ports:
28-
- 8000
2927

3028
environment:
3129
INVENTREE_DB_ENGINE: postgresql

src/backend/InvenTree/InvenTree/helpers_email.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.core import mail as django_mail
55

66
import structlog
7+
from allauth.account.models import EmailAddress
78

89
import InvenTree.ready
910
import InvenTree.tasks
@@ -56,7 +57,6 @@ def send_email(subject, body, recipients, from_email=None, html_message=None):
5657
recipients = [recipients]
5758

5859
import InvenTree.ready
59-
import InvenTree.status
6060

6161
if InvenTree.ready.isImportingData():
6262
# 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):
8989
html_message=html_message,
9090
group='notification',
9191
)
92+
93+
94+
def get_email_for_user(user) -> str:
95+
"""Find an email address for the specified user."""
96+
# First check if the user has an associated email address
97+
if user.email:
98+
return user.email
99+
100+
# Otherwise, find first matching email
101+
# Priority is given to primary or verified email addresses
102+
if (
103+
email := EmailAddress.objects.filter(user=user)
104+
.order_by('-primary', '-verified')
105+
.first()
106+
):
107+
return email.email

src/backend/InvenTree/InvenTree/tasks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,4 +705,7 @@ def email_user(user_id: int, subject: str, message: str) -> None:
705705
logger.warning('User <%s> not found - cannot send welcome message', user_id)
706706
return
707707

708-
user.email_user(subject=subject, message=message)
708+
from InvenTree.helpers_email import get_email_for_user, send_email
709+
710+
if email := get_email_for_user(user):
711+
send_email(subject, message, [email])

0 commit comments

Comments
 (0)