Simplify email subject handling and remove duplicate functions
Mirror and run GitLab CI / build (push) Has been cancelled
Ruff / ruff (push) Has been cancelled

- Remove send_success_email/send_error_email from email_sender.py
- Centralize subject logic in tasks.py using _get_subject()
- Use EMAIL_SUBJECT_SUCCESS and EMAIL_SUBJECT_ERROR with clear defaults
- Ensure subject is always set and logged before sending
This commit is contained in:
admin
2026-06-15 03:52:19 +00:00
parent e27e5b8522
commit b0a1bc059b
2 changed files with 4 additions and 125 deletions
-109
View File
@@ -455,112 +455,3 @@ def create_summary_docx(
_add_summary_content(doc, text)
doc.save(filename)
def send_success_email(
to: str,
transcript_text: str,
summary_text: str,
attachments: List[str],
task_id: str,
):
"""
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.
"""
# 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"""
Your transcription is complete.
Task ID: {task_id}
Please find the attached documents:
- Transcript (MD)
- Transcript (DOCX)
- Source JSON
"""
if summary_text:
body += "- Summary (MD)\n- Summary (DOCX)\n"
# Load HTML template
try:
html = load_template(
"success_template.html",
task_id=task_id,
transcript_text=transcript_text[:500],
summary_text=summary_text[:500] if summary_text else "",
)
except EmailError:
html = None
# Send email (send_email has an additional subject guard)
send_email(
to=to,
subject=subject,
body=body,
html=html,
attachments=attachments,
)
def send_error_email(
to: str,
error_message: str,
task_id: str,
):
"""
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.
"""
# 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"""
There was an error processing your transcription.
Task ID: {task_id}
Error: {error_message}
"""
# Load HTML template
try:
html = load_template(
"error_notification_template.html",
task_id=task_id,
error_message=error_message,
)
except EmailError:
html = None
# Send email (send_email has an additional subject guard)
send_email(
to=to,
subject=subject,
body=body,
html=html,
attachments=[],
)
+3 -15
View File
@@ -164,22 +164,10 @@ def send_success_email(
Subject is customizable via EMAIL_SUBJECT_SUCCESS.
Falls back to a safe default if the env var is missing or blank.
"""
# 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
subject = _get_subject(
"EMAIL_SUBJECT_SUCCESS",
"ScrAIbe: Your transcript is ready",
)
else:
logger.info("Using EMAIL_SUBJECT_SUCCESS: %s", subject)
# Final guard: ensure subject is never empty
if not subject:
subject = "ScrAIbe: Your transcript is ready"
logger.warning("Subject was empty after reading; forced default subject.")
body = (
"Hello,\n\n"