# utils/otp_utils.py import random from django.utils import timezone from datetime import timedelta from django.conf import settings from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string from django.utils.html import strip_tags import logging logger = logging.getLogger(__name__) def generate_otp(): return str(random.randint(100000, 999999)) def send_otp_via_email(email, otp, user_name=None, context='registration'): try: context_data = { 'user_name': user_name or 'User', 'otp': otp, 'expiry_minutes': 10, 'company_name': 'Attune Heart Therapy', 'support_email': 'admin@attunehearttherapy.com', 'current_year': timezone.now().year, 'email_context': context } if context == 'password_reset': template_name = 'emails/password_reset_otp.html' subject = 'Password Reset Request - Verification Code' else: template_name = 'emails/otp_verification.html' subject = 'Your Verification Code - Secure Your Account' html_content = render_to_string(template_name, context_data) text_content = strip_tags(html_content) from_email = settings.DEFAULT_FROM_EMAIL to_email = [email] email_msg = EmailMultiAlternatives( subject, text_content, from_email, to_email ) email_msg.attach_alternative(html_content, "text/html") email_msg.send() return True except Exception as e: print(f"Error sending email OTP: {e}") return False def is_otp_expired(otp_expiry): if otp_expiry and timezone.now() < otp_expiry: return False return True def send_email_notifications(contact_message): try: send_admin_notification(contact_message) send_user_confirmation(contact_message) except Exception as e: logger.error(f"Error sending email notifications: {str(e)}") def send_admin_notification(contact_message): subject = f"New Contact Form Submission from {contact_message.name}" html_content = render_to_string('emails/admin_contact_notification.html', { 'contact_message': contact_message }) email = EmailMultiAlternatives( subject=subject, body=html_content, from_email=settings.DEFAULT_FROM_EMAIL, to=[settings.DEFAULT_FROM_EMAIL] ) email.content_subtype = 'html' email.send() def send_user_confirmation(self, contact_message): subject = "Thank you for contacting us" html_content = render_to_string('emails/user_contact_confirmation.html', { 'contact_message': contact_message, 'company_name': 'Attune Heart Therapy', 'support_email': 'admin@attunehearttherapy.com', 'current_year': timezone.now().year, }) email = EmailMultiAlternatives( subject=subject, body=html_content, from_email=settings.DEFAULT_FROM_EMAIL, to=[contact_message.email] ) email.content_subtype = 'html' email.send()