Ensure success and error emails always have a subject
Mirror and run GitLab CI / build (push) Has been cancelled
Ruff / ruff (push) Has been cancelled

- Use EMAIL_SUBJECT_SUCCESS env var for success emails
- Use EMAIL_SUBJECT_ERROR env var for error emails
- Provide safe defaults if env vars are missing or blank
- Add final guard in send_email() to prevent blank subjects
This commit is contained in:
admin
2026-06-15 02:57:09 +00:00
parent 42a155aeaa
commit 46fbcf80af
+40 -22
View File
@@ -218,19 +218,20 @@ def send_email(
if not to_list: if not to_list:
raise EmailError("No valid 'To' email addresses provided.") raise EmailError("No valid 'To' email addresses provided.")
# Ensure subject is never blank
if not subject or not subject.strip():
logger.warning("Subject was blank or missing; using default subject.")
subject = "ScrAIbe: Your transcript is ready"
subject = subject.strip()
# Build message # Build message
msg = MIMEMultipart("alternative") msg = MIMEMultipart("alternative")
msg["From"] = cfg["from_address"] msg["From"] = cfg["from_address"]
msg["To"] = ", ".join(to_list) msg["To"] = ", ".join(to_list)
if cc_list: if cc_list:
msg["Cc"] = ", ".join(cc_list) msg["Cc"] = ", ".join(cc_list)
msg["Subject"] = subject
# Ensure subject is never blank
if not subject or not subject.strip():
logger.warning("Subject was blank; using default subject.")
subject = "ScrAIbe: Your transcript is ready"
msg["Subject"] = subject.strip()
# Attach plain text # Attach plain text
msg.attach(MIMEText(body, "plain")) msg.attach(MIMEText(body, "plain"))
@@ -283,9 +284,10 @@ def send_email(
) )
server.quit() server.quit()
logger.info( logger.info(
"Email sent to %s (CC: %s)", "Email sent to %s (CC: %s) with subject: %s",
to_list, to_list,
cc_list or "None", cc_list or "None",
subject,
) )
return True return True
@@ -810,12 +812,20 @@ def send_success_email(
): ):
""" """
Send a success email with attachments. Send a success email with attachments.
Subject is customizable via EMAIL_SUBJECT_SUCCESS.
Falls back to a safe default if the env var is missing or blank.
""" """
try: # Read subject from environment; never allow blank
cfg = get_email_config() raw_subject = os.getenv("EMAIL_SUBJECT_SUCCESS")
except EmailError as e: subject = (raw_subject or "").strip()
logger.error("Email configuration error: %s", e)
raise if not subject:
subject = "ScrAIbe: Your transcript is ready"
logger.info(
"EMAIL_SUBJECT_SUCCESS not set or blank; using default subject: %s", subject
)
else:
logger.info("Using EMAIL_SUBJECT_SUCCESS: %s", subject)
# Build email body # Build email body
body = f""" body = f"""
@@ -842,10 +852,10 @@ Please find the attached documents:
except EmailError: except EmailError:
html = None html = None
# Send email # Send email (send_email has an additional subject guard)
send_email( send_email(
to=to, to=to,
subject=f"Transcription Complete - Task {task_id}", subject=subject,
body=body, body=body,
html=html, html=html,
attachments=attachments, attachments=attachments,
@@ -859,12 +869,20 @@ def send_error_email(
): ):
""" """
Send an error email. Send an error email.
Subject is customizable via EMAIL_SUBJECT_ERROR.
Falls back to a safe default if the env var is missing or blank.
""" """
try: # Read subject from environment; never allow blank
cfg = get_email_config() raw_subject = os.getenv("EMAIL_SUBJECT_ERROR")
except EmailError as e: subject = (raw_subject or "").strip()
logger.error("Email configuration error: %s", e)
raise if not subject:
subject = "ScrAIbe: Error with your transcription request"
logger.info(
"EMAIL_SUBJECT_ERROR not set or blank; using default subject: %s", subject
)
else:
logger.info("Using EMAIL_SUBJECT_ERROR: %s", subject)
# Build email body # Build email body
body = f""" body = f"""
@@ -884,10 +902,10 @@ Error: {error_message}
except EmailError: except EmailError:
html = None html = None
# Send email # Send email (send_email has an additional subject guard)
send_email( send_email(
to=to, to=to,
subject=f"Transcription Error - Task {task_id}", subject=subject,
body=body, body=body,
html=html, html=html,
attachments=[], attachments=[],