Squash merge feature/email-notifications into beta
This commit is contained in:
53
claims/email_utils.py
Normal file
53
claims/email_utils.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import send_mail
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.html import strip_tags
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def emails_enabled():
|
||||
return getattr(settings, "CLAIMS_EMAIL_ENABLED", False)
|
||||
|
||||
|
||||
def _send(subject: str, template: str, context: dict, recipient: str):
|
||||
if not emails_enabled():
|
||||
logger.info("Email disabled - skipping send to %s", recipient)
|
||||
return
|
||||
html_message = render_to_string(template, context)
|
||||
plain_message = strip_tags(html_message)
|
||||
send_mail(
|
||||
subject=subject,
|
||||
message=plain_message,
|
||||
from_email=settings.CLAIMS_EMAIL_FROM,
|
||||
recipient_list=[recipient],
|
||||
html_message=html_message,
|
||||
fail_silently=False,
|
||||
)
|
||||
|
||||
|
||||
def send_claimant_confirmation_email(claim):
|
||||
if not claim.email:
|
||||
return
|
||||
context = {"claim": claim}
|
||||
_send(
|
||||
subject="Din utläggsbegäran är mottagen",
|
||||
template="emails/claim_submitted_claimant.html",
|
||||
context=context,
|
||||
recipient=claim.email,
|
||||
)
|
||||
|
||||
|
||||
def notify_admin_of_claim(claim):
|
||||
recipient = settings.CLAIMS_ADMIN_NOTIFICATION_EMAIL
|
||||
if not recipient:
|
||||
return
|
||||
context = {"claim": claim}
|
||||
_send(
|
||||
subject="Nytt utlägg inskickat",
|
||||
template="emails/claim_submitted_admin.html",
|
||||
context=context,
|
||||
recipient=recipient,
|
||||
)
|
||||
Reference in New Issue
Block a user