Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -10,7 +10,7 @@ from flask_apscheduler import APScheduler
|
|
| 10 |
import firebase_admin
|
| 11 |
from firebase_admin import credentials, db, auth as firebase_auth
|
| 12 |
from resend import Emails
|
| 13 |
-
from resend.exceptions import ResendError # <--
|
| 14 |
import logging
|
| 15 |
from logging.handlers import RotatingFileHandler
|
| 16 |
import time
|
|
@@ -36,10 +36,11 @@ root_logger.addHandler(file_handler)
|
|
| 36 |
root_logger.addHandler(console_handler)
|
| 37 |
|
| 38 |
# === ENV Config ===
|
|
|
|
|
|
|
| 39 |
RESEND_API_KEY = os.getenv("RESEND_API_KEY")
|
| 40 |
FIREBASE_CRED_JSON = json.loads(os.getenv("FIREBASE"))
|
| 41 |
FIREBASE_DB_URL = os.getenv("Firebase_DB")
|
| 42 |
-
ADMIN_EMAILS = ["rairorr@gmail.com", "nharingosheperd@gmail.com"]
|
| 43 |
logger.info("Environment variables loaded.")
|
| 44 |
|
| 45 |
# === Firebase Init ===
|
|
@@ -62,11 +63,11 @@ Emails.api_key = RESEND_API_KEY
|
|
| 62 |
logger.info("Flask, APScheduler, and Resend initialized.")
|
| 63 |
|
| 64 |
|
| 65 |
-
# ===
|
| 66 |
def send_email(to, subject, html):
|
| 67 |
-
"""Sends an email with sanitized input and
|
| 68 |
try:
|
| 69 |
-
#
|
| 70 |
clean_to = to.strip()
|
| 71 |
if not clean_to:
|
| 72 |
logger.error("Attempted to send email to an empty address.")
|
|
@@ -83,17 +84,14 @@ def send_email(to, subject, html):
|
|
| 83 |
return None
|
| 84 |
logger.info(f"Email sent successfully to {clean_to}. Response ID: {response.get('id')}")
|
| 85 |
return response
|
| 86 |
-
# --- FIX: More specific error catching for Resend ---
|
| 87 |
except ResendError as e:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
exc_info=True
|
| 92 |
-
)
|
| 93 |
return None
|
| 94 |
-
except Exception:
|
| 95 |
# Fallback for any other unexpected errors
|
| 96 |
-
logger.error(f"An unexpected exception occurred while sending email to '{to}'", exc_info=True)
|
| 97 |
return None
|
| 98 |
|
| 99 |
|
|
@@ -258,6 +256,7 @@ def log_response_info(response):
|
|
| 258 |
def home():
|
| 259 |
return jsonify({"message": "Rairo Guards API is running.", "status": "ok"}), 200
|
| 260 |
|
|
|
|
| 261 |
@app.route("/upload_members", methods=["POST"])
|
| 262 |
def upload_members():
|
| 263 |
if not verify_token(request): return jsonify({"error": "Unauthorized"}), 401
|
|
|
|
| 10 |
import firebase_admin
|
| 11 |
from firebase_admin import credentials, db, auth as firebase_auth
|
| 12 |
from resend import Emails
|
| 13 |
+
from resend.exceptions import ResendError # <-- Keep this import
|
| 14 |
import logging
|
| 15 |
from logging.handlers import RotatingFileHandler
|
| 16 |
import time
|
|
|
|
| 36 |
root_logger.addHandler(console_handler)
|
| 37 |
|
| 38 |
# === ENV Config ===
|
| 39 |
+
# CRITICAL: Double-check this list in your environment or code for hidden spaces!
|
| 40 |
+
ADMIN_EMAILS = ["rairorr@gmail.com", "nharingosheperd@gmail.com"]
|
| 41 |
RESEND_API_KEY = os.getenv("RESEND_API_KEY")
|
| 42 |
FIREBASE_CRED_JSON = json.loads(os.getenv("FIREBASE"))
|
| 43 |
FIREBASE_DB_URL = os.getenv("Firebase_DB")
|
|
|
|
| 44 |
logger.info("Environment variables loaded.")
|
| 45 |
|
| 46 |
# === Firebase Init ===
|
|
|
|
| 63 |
logger.info("Flask, APScheduler, and Resend initialized.")
|
| 64 |
|
| 65 |
|
| 66 |
+
# === DEFINITIVELY FIXED FUNCTION (send_email) ===
|
| 67 |
def send_email(to, subject, html):
|
| 68 |
+
"""Sends an email with sanitized input and robust error logging."""
|
| 69 |
try:
|
| 70 |
+
# Sanitize the email address to remove leading/trailing whitespace
|
| 71 |
clean_to = to.strip()
|
| 72 |
if not clean_to:
|
| 73 |
logger.error("Attempted to send email to an empty address.")
|
|
|
|
| 84 |
return None
|
| 85 |
logger.info(f"Email sent successfully to {clean_to}. Response ID: {response.get('id')}")
|
| 86 |
return response
|
|
|
|
| 87 |
except ResendError as e:
|
| 88 |
+
# --- FIX: Convert the entire exception to a string. ---
|
| 89 |
+
# This is robust and will not crash even if attributes are missing.
|
| 90 |
+
logger.error(f"A Resend API error occurred for recipient '{to}'. The error is: {str(e)}", exc_info=True)
|
|
|
|
|
|
|
| 91 |
return None
|
| 92 |
+
except Exception as e:
|
| 93 |
# Fallback for any other unexpected errors
|
| 94 |
+
logger.error(f"An unexpected exception occurred while sending email to '{to}': {str(e)}", exc_info=True)
|
| 95 |
return None
|
| 96 |
|
| 97 |
|
|
|
|
| 256 |
def home():
|
| 257 |
return jsonify({"message": "Rairo Guards API is running.", "status": "ok"}), 200
|
| 258 |
|
| 259 |
+
# (All your other routes like /upload_members, /create_job, etc. remain here unchanged)
|
| 260 |
@app.route("/upload_members", methods=["POST"])
|
| 261 |
def upload_members():
|
| 262 |
if not verify_token(request): return jsonify({"error": "Unauthorized"}), 401
|