Files
azionelab/backend/bookings/emailing.py
2026-04-29 23:39:12 +02:00

62 lines
1.8 KiB
Python

import logging
from django.conf import settings
from django.core.mail import send_mail
logger = logging.getLogger(__name__)
CONFIRMATION_PATH = "/api/reservations/confirm/"
def build_confirmation_link(raw_confirmation_token):
return f"{settings.SITE_BASE_URL}{CONFIRMATION_PATH}?token={raw_confirmation_token}"
def _log_confirmation_link_for_local_debug(*, reservation, confirmation_link, reason):
if not settings.LOG_RESERVATION_CONFIRMATION_URLS:
return
logger.info(
"Local reservation confirmation link for manual testing (%s) for reservation %s: %s",
reason,
reservation.id,
confirmation_link,
)
def send_confirmation_email(*, reservation, raw_confirmation_token):
confirmation_link = build_confirmation_link(raw_confirmation_token)
subject = f"Confirm your reservation for {reservation.performance.show.title}"
message = (
"Thank you for your reservation request.\n\n"
"Please confirm your reservation by opening this link:\n"
f"{confirmation_link}\n\n"
"If you did not request this reservation, you can ignore this email."
)
_log_confirmation_link_for_local_debug(
reservation=reservation,
confirmation_link=confirmation_link,
reason="email send attempt",
)
try:
send_mail(
subject=subject,
message=message,
from_email=None,
recipient_list=[reservation.email],
fail_silently=False,
)
except Exception:
_log_confirmation_link_for_local_debug(
reservation=reservation,
confirmation_link=confirmation_link,
reason="email send failure fallback",
)
logger.exception(
"Failed to send confirmation email for reservation %s.",
reservation.id,
)