Merge branch 'main' of http://10.10.1.136:3000/marwan/kaauh_ats into frontend
This commit is contained in:
commit
9b2bf34431
@ -360,86 +360,78 @@ def send_bulk_email(subject, message, recipient_list, request=None, attachments=
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
# --- 3. Handle SYNCHRONOUS Send (No changes needed here, as it was fixed previously) ---
|
# --- 3. Handle SYNCHRONOUS Send (No changes needed here, as it was fixed previously) ---
|
||||||
try:
|
try:
|
||||||
# NOTE: The synchronous block below should also use the 'customized_sends'
|
# NOTE: The synchronous block below should also use the 'customized_sends'
|
||||||
# list for consistency instead of rebuilding messages from 'pure_candidate_emails'
|
# list for consistency instead of rebuilding messages from 'pure_candidate_emails'
|
||||||
# and 'agency_emails', but keeping your current logic structure to minimize changes.
|
# and 'agency_emails', but keeping your current logic structure to minimize changes.
|
||||||
|
|
||||||
from_email = getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@kaauh.edu.sa')
|
from_email = getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@kaauh.edu.sa')
|
||||||
is_html = '<' in message and '>' in message
|
is_html = '<' in message and '>' in message
|
||||||
successful_sends = 0
|
successful_sends = 0
|
||||||
try:
|
|
||||||
# NOTE: The synchronous block below should also use the 'customized_sends'
|
|
||||||
# list for consistency instead of rebuilding messages from 'pure_candidate_emails'
|
|
||||||
# and 'agency_emails', but keeping your current logic structure to minimize changes.
|
|
||||||
|
|
||||||
from_email = getattr(settings, 'DEFAULT_FROM_EMAIL', 'noreply@kaauh.edu.sa')
|
# Helper Function for Sync Send (as provided)
|
||||||
is_html = '<' in message and '>' in message
|
def send_individual_email(recipient, body_message):
|
||||||
successful_sends = 0
|
# ... (Existing helper function logic) ...
|
||||||
|
nonlocal successful_sends
|
||||||
|
|
||||||
# Helper Function for Sync Send (as provided)
|
if is_html:
|
||||||
def send_individual_email(recipient, body_message):
|
plain_message = strip_tags(body_message)
|
||||||
# ... (Existing helper function logic) ...
|
email_obj = EmailMultiAlternatives(subject=subject, body=plain_message, from_email=from_email, to=[recipient])
|
||||||
nonlocal successful_sends
|
email_obj.attach_alternative(body_message, "text/html")
|
||||||
|
else:
|
||||||
|
email_obj = EmailMultiAlternatives(subject=subject, body=body_message, from_email=from_email, to=[recipient])
|
||||||
|
|
||||||
if is_html:
|
if attachments:
|
||||||
plain_message = strip_tags(body_message)
|
for attachment in attachments:
|
||||||
email_obj = EmailMultiAlternatives(subject=subject, body=plain_message, from_email=from_email, to=[recipient])
|
if hasattr(attachment, 'read'):
|
||||||
email_obj.attach_alternative(body_message, "text/html")
|
filename = getattr(attachment, 'name', 'attachment')
|
||||||
|
content = attachment.read()
|
||||||
|
content_type = getattr(attachment, 'content_type', 'application/octet-stream')
|
||||||
|
email_obj.attach(filename, content, content_type)
|
||||||
|
elif isinstance(attachment, tuple) and len(attachment) == 3:
|
||||||
|
filename, content, content_type = attachment
|
||||||
|
email_obj.attach(filename, content, content_type)
|
||||||
|
|
||||||
|
try:
|
||||||
|
email_obj.send(fail_silently=False)
|
||||||
|
successful_sends += 1
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to send email to {recipient}: {str(e)}", exc_info=True)
|
||||||
|
|
||||||
|
if not from_interview:
|
||||||
|
# Send Emails - Pure Candidates
|
||||||
|
for email in pure_candidate_emails:
|
||||||
|
candidate_name = Application.objects.filter(email=email).first().first_name
|
||||||
|
candidate_message = f"Hi, {candidate_name}" + "\n" + message
|
||||||
|
send_individual_email(email, candidate_message)
|
||||||
|
|
||||||
|
# Send Emails - Agencies
|
||||||
|
i = 0
|
||||||
|
for email in agency_emails:
|
||||||
|
candidate_email = candidate_through_agency_emails[i]
|
||||||
|
candidate_name = Application.objects.filter(email=candidate_email).first().first_name
|
||||||
|
agency_message = f"Hi, {candidate_name}" + "\n" + message
|
||||||
|
send_individual_email(email, agency_message)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
logger.info(f"Bulk email processing complete. Sent successfully to {successful_sends} out of {total_recipients} unique recipients.")
|
||||||
|
return {
|
||||||
|
'success': True,
|
||||||
|
'recipients_count': successful_sends,
|
||||||
|
'message': f'Email processing complete. {successful_sends} email(s) were sent successfully to {total_recipients} unique intended recipients.'
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
email_obj = EmailMultiAlternatives(subject=subject, body=body_message, from_email=from_email, to=[recipient])
|
for email in recipient_list:
|
||||||
|
send_individual_email(email, message)
|
||||||
|
|
||||||
if attachments:
|
logger.info(f"Interview email processing complete. Sent successfully to {successful_sends} out of {total_recipients} recipients.")
|
||||||
for attachment in attachments:
|
return {
|
||||||
if hasattr(attachment, 'read'):
|
'success': True,
|
||||||
filename = getattr(attachment, 'name', 'attachment')
|
'recipients_count': successful_sends,
|
||||||
content = attachment.read()
|
'message': f'Interview emails sent successfully to {successful_sends} recipient(s).'
|
||||||
content_type = getattr(attachment, 'content_type', 'application/octet-stream')
|
}
|
||||||
email_obj.attach(filename, content, content_type)
|
|
||||||
elif isinstance(attachment, tuple) and len(attachment) == 3:
|
|
||||||
filename, content, content_type = attachment
|
|
||||||
email_obj.attach(filename, content, content_type)
|
|
||||||
|
|
||||||
try:
|
except Exception as e:
|
||||||
email_obj.send(fail_silently=False)
|
error_msg = f"Failed to process bulk email send request: {str(e)}"
|
||||||
successful_sends += 1
|
logger.error(error_msg, exc_info=True)
|
||||||
except Exception as e:
|
return {'success': False, 'error': error_msg}
|
||||||
logger.error(f"Failed to send email to {recipient}: {str(e)}", exc_info=True)
|
|
||||||
|
|
||||||
if not from_interview:
|
|
||||||
# Send Emails - Pure Candidates
|
|
||||||
for email in pure_candidate_emails:
|
|
||||||
candidate_name = Application.objects.filter(email=email).first().first_name
|
|
||||||
candidate_message = f"Hi, {candidate_name}" + "\n" + message
|
|
||||||
send_individual_email(email, candidate_message)
|
|
||||||
|
|
||||||
# Send Emails - Agencies
|
|
||||||
i = 0
|
|
||||||
for email in agency_emails:
|
|
||||||
candidate_email = candidate_through_agency_emails[i]
|
|
||||||
candidate_name = Application.objects.filter(email=candidate_email).first().first_name
|
|
||||||
agency_message = f"Hi, {candidate_name}" + "\n" + message
|
|
||||||
send_individual_email(email, agency_message)
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
logger.info(f"Bulk email processing complete. Sent successfully to {successful_sends} out of {total_recipients} unique recipients.")
|
|
||||||
return {
|
|
||||||
'success': True,
|
|
||||||
'recipients_count': successful_sends,
|
|
||||||
'message': f'Email processing complete. {successful_sends} email(s) were sent successfully to {total_recipients} unique intended recipients.'
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
for email in recipient_list:
|
|
||||||
send_individual_email(email, message)
|
|
||||||
|
|
||||||
logger.info(f"Interview email processing complete. Sent successfully to {successful_sends} out of {total_recipients} recipients.")
|
|
||||||
return {
|
|
||||||
'success': True,
|
|
||||||
'recipients_count': successful_sends,
|
|
||||||
'message': f'Interview emails sent successfully to {successful_sends} recipient(s).'
|
|
||||||
}
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
error_msg = f"Failed to process bulk email send request: {str(e)}"
|
|
||||||
logger.error(error_msg, exc_info=True)
|
|
||||||
return {'success': False, 'error': error_msg}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user