Simplify email subject handling and remove duplicate functions
- 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:
@@ -455,112 +455,3 @@ def create_summary_docx(
|
|||||||
_add_summary_content(doc, text)
|
_add_summary_content(doc, text)
|
||||||
|
|
||||||
doc.save(filename)
|
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=[],
|
|
||||||
)
|
|
||||||
|
|||||||
+4
-16
@@ -164,22 +164,10 @@ def send_success_email(
|
|||||||
Subject is customizable via EMAIL_SUBJECT_SUCCESS.
|
Subject is customizable via EMAIL_SUBJECT_SUCCESS.
|
||||||
Falls back to a safe default if the env var is missing or blank.
|
Falls back to a safe default if the env var is missing or blank.
|
||||||
"""
|
"""
|
||||||
# Read subject from environment; never allow blank
|
subject = _get_subject(
|
||||||
raw_subject = os.getenv("EMAIL_SUBJECT_SUCCESS")
|
"EMAIL_SUBJECT_SUCCESS",
|
||||||
subject = (raw_subject or "").strip()
|
"ScrAIbe: Your transcript is ready",
|
||||||
|
)
|
||||||
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)
|
|
||||||
|
|
||||||
# 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 = (
|
body = (
|
||||||
"Hello,\n\n"
|
"Hello,\n\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user