mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-27 11:06: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:
parent
fd4caceed0
commit
afbcfe66bb
@ -24,8 +24,6 @@ services:
|
||||
volumes:
|
||||
- ../:/home/inventree:z
|
||||
- /tmp/.X11-unix:/tmp/.X11-unix
|
||||
ports:
|
||||
- 8000
|
||||
|
||||
environment:
|
||||
INVENTREE_DB_ENGINE: postgresql
|
||||
|
@ -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
|
||||
|
@ -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])
|
||||
|
Loading…
x
Reference in New Issue
Block a user