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:
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
msg = MIMEMultipart("alternative")
msg["From"] = cfg["from_address"]
msg["To"] = ", ".join(to_list)
if cc_list:
msg["Cc"] = ", ".join(cc_list)
# 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()
msg["Subject"] = subject
# Attach plain text
msg.attach(MIMEText(body, "plain"))
@@ -283,9 +284,10 @@ def send_email(
)
server.quit()
logger.info(
"Email sent to %s (CC: %s)",
"Email sent to %s (CC: %s) with subject: %s",
to_list,
cc_list or "None",
subject,
)
return True
@@ -810,12 +812,20 @@ def send_success_email(
):
"""
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:
cfg = get_email_config()
except EmailError as e:
logger.error("Email configuration error: %s", e)
raise
# Read subject from environment; never allow blank
raw_subject = os.getenv("EMAIL_SUBJECT_SUCCESS")
subject = (raw_subject or "").strip()
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
body = f"""
@@ -842,10 +852,10 @@ Please find the attached documents:
except EmailError:
html = None
# Send email
# Send email (send_email has an additional subject guard)
send_email(
to=to,
subject=f"Transcription Complete - Task {task_id}",
subject=subject,
body=body,
html=html,
attachments=attachments,
@@ -859,12 +869,20 @@ def send_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:
cfg = get_email_config()
except EmailError as e:
logger.error("Email configuration error: %s", e)
raise
# Read subject from environment; never allow blank
raw_subject = os.getenv("EMAIL_SUBJECT_ERROR")
subject = (raw_subject or "").strip()
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
body = f"""
@@ -884,10 +902,10 @@ Error: {error_message}
except EmailError:
html = None
# Send email
# Send email (send_email has an additional subject guard)
send_email(
to=to,
subject=f"Transcription Error - Task {task_id}",
subject=subject,
body=body,
html=html,
attachments=[],