From 46fbcf80afca5cd64bc46005e5a0fa601eb00cca Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 15 Jun 2026 02:57:09 +0000 Subject: [PATCH] Ensure success and error emails always have a subject - 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 --- scraibe/email_sender.py | 62 ++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/scraibe/email_sender.py b/scraibe/email_sender.py index d04dd2e..5041107 100644 --- a/scraibe/email_sender.py +++ b/scraibe/email_sender.py @@ -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=[],