import logging from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string logger = logging.getLogger(__name__) def send_email(template_name: str, context: dict, subject: str, to: str | list[str]) -> bool: """ Render and send an HTML email with plaintext fallback. Returns True on success, False on failure. """ if isinstance(to, str): to = [to] html = render_to_string(f'emails/{template_name}.html', context) text = render_to_string(f'emails/{template_name}.txt', context) msg = EmailMultiAlternatives( subject=subject, body=text, from_email=settings.DEFAULT_FROM_EMAIL, to=to, ) msg.attach_alternative(html, 'text/html') try: msg.send() return True except Exception: logger.exception('Failed to send email "%s" to %s', template_name, to) return False