""" Email service for sending notifications related to agency messaging. """ from django.core.mail import send_mail from django.conf import settings from django.template.loader import render_to_string from django.utils.html import strip_tags import logging logger = logging.getLogger(__name__) class EmailService: """ Service class for handling email notifications """ def send_email(self, recipient_email, subject, body, html_body=None): """ Send email using Django's send_mail function Args: recipient_email: Email address to send to subject: Email subject body: Plain text email body html_body: HTML email body (optional) Returns: dict: Result with success status and error message if failed """ try: send_mail( subject=subject, message=body, from_email=getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@kaauh.edu.sa'), recipient_list=[recipient_email], html_message=html_body, fail_silently=False, ) logger.info(f"Email sent successfully to {recipient_email}") return {'success': True} except Exception as e: error_msg = f"Failed to send email to {recipient_email}: {str(e)}" logger.error(error_msg) return {'success': False, 'error': error_msg} def send_agency_welcome_email(agency, access_link=None): """ Send welcome email to a new agency with portal access information. Args: agency: HiringAgency instance access_link: AgencyAccessLink instance (optional) Returns: bool: True if email was sent successfully, False otherwise """ try: if not agency.email: logger.warning(f"No email found for agency {agency.id}") return False context = { 'agency': agency, 'access_link': access_link, 'portal_url': getattr(settings, 'AGENCY_PORTAL_URL', 'https://kaauh.edu.sa/portal/'), } # Render email templates html_message = render_to_string('recruitment/emails/agency_welcome.html', context) plain_message = strip_tags(html_message) # Send email send_mail( subject='Welcome to KAAUH Recruitment Portal', message=plain_message, from_email=getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@kaauh.edu.sa'), recipient_list=[agency.email], html_message=html_message, fail_silently=False, ) logger.info(f"Welcome email sent to agency {agency.email}") return True except Exception as e: logger.error(f"Failed to send agency welcome email: {str(e)}") return False def send_assignment_notification_email(assignment, message_type='created'): """ Send email notification about assignment changes. Args: assignment: AgencyJobAssignment instance message_type: Type of notification ('created', 'updated', 'deadline_extended') Returns: bool: True if email was sent successfully, False otherwise """ try: if not assignment.agency.email: logger.warning(f"No email found for agency {assignment.agency.id}") return False context = { 'assignment': assignment, 'agency': assignment.agency, 'job': assignment.job, 'message_type': message_type, 'portal_url': getattr(settings, 'AGENCY_PORTAL_URL', 'https://kaauh.edu.sa/portal/'), } # Render email templates html_message = render_to_string('recruitment/emails/assignment_notification.html', context) plain_message = strip_tags(html_message) # Determine subject based on message type subjects = { 'created': f'New Job Assignment: {assignment.job.title}', 'updated': f'Assignment Updated: {assignment.job.title}', 'deadline_extended': f'Deadline Extended: {assignment.job.title}', } subject = subjects.get(message_type, f'Assignment Notification: {assignment.job.title}') # Send email send_mail( subject=subject, message=plain_message, from_email=getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@kaauh.edu.sa'), recipient_list=[assignment.agency.email], html_message=html_message, fail_silently=False, ) logger.info(f"Assignment notification email sent to {assignment.agency.email} for {message_type}") return True except Exception as e: logger.error(f"Failed to send assignment notification email: {str(e)}") return False