Spaces:
Paused
Paused
Download core/email.py from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 4.95 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/core/email.py
- Command line
-
hf download hf://spaces/3SCError404/miracle/core/email.py
-
curl -L -o email.py https://huggingface.co/spaces/3SCError404/miracle/resolve/main/core/email.py
4.95 kB
| """ | |
| Email Utility for Resend integration. | |
| Uses Resend for real-time delivery with a local forensic log fallback. | |
| """ | |
| import aiofiles | |
| import os | |
| import asyncio | |
| import resend | |
| from datetime import datetime | |
| from core.logging import get_logger | |
| from core.config import get_settings | |
| logger = get_logger(__name__) | |
| async def send_email(to_email: str, subject: str, body: str, html_body: str = None): | |
| """ | |
| Asynchronous email delivery using Resend. | |
| Always logs the payload to /logs/emails.log for forensic verification. | |
| """ | |
| settings = get_settings() | |
| log_dir = settings.effective_log_dir | |
| log_file = os.path.join(log_dir, "emails.log") | |
| if not os.path.exists(log_dir): | |
| os.makedirs(log_dir) | |
| html_log_part = f"BODY (HTML):\n{html_body}" if html_body else "" | |
| email_payload = f""" | |
| {"="*60} | |
| {"[LIVE]" if settings.RESEND_API_KEY else "[SIMULATED]"} | |
| TIMESTAMP: {datetime.utcnow().isoformat()} | |
| TO: {to_email} | |
| SUBJECT: {subject} | |
| BODY (TEXT): | |
| {body} | |
| {html_log_part} | |
| {"="*60} | |
| """ | |
| # 1. Forensic Logging (Always) | |
| try: | |
| async with aiofiles.open(log_file, mode="a", encoding="utf-8") as f: | |
| await f.write(email_payload) | |
| except Exception as e: | |
| logger.error(f"Failed to write to email forensic log: {str(e)}") | |
| # 2. Resend Delivery (Conditional) | |
| if not settings.RESEND_API_KEY: | |
| logger.info(f"SIMULATED EMAIL SENT TO: {to_email} | SUBJECT: {subject} (No Resend API Key)") | |
| return True | |
| try: | |
| resend.api_key = settings.RESEND_API_KEY | |
| params = { | |
| "from": settings.RESEND_FROM_EMAIL, | |
| "to": [to_email], | |
| "subject": subject, | |
| "text": body, | |
| } | |
| if html_body: | |
| params["html"] = html_body | |
| # Native thread-pool handoff (avoids deprecated get_event_loop) | |
| response = await asyncio.to_thread(resend.Emails.send, params) | |
| logger.info(f"RESEND EMAIL SENT TO: {to_email} | ID: {response.get('id', 'N/A')}") | |
| return True | |
| except Exception as e: | |
| logger.error(f"FAILED TO SEND RESEND EMAIL: {str(e)}") | |
| # We already have the forensic log as fallback | |
| return False | |
| def template_nexus_email(title: str, message: str, action_link: str = None, action_label: str = None, footer_note: str = None) -> str: | |
| """Returns a highly professional HTML template for Nexus emails.""" | |
| settings = get_settings() | |
| logo_url = settings.APP_LOGO_URL | |
| bg_color = "#0F172A" # Dark Navy (Nexus Theme) | |
| brand_color = "#6366F1" # Indigo 500 | |
| action_button = "" | |
| if action_link and action_label: | |
| action_button = f''' | |
| <div style="margin: 32px 0; text-align: center;"> | |
| <a href="{action_link}" style="background-color: {brand_color}; color: #ffffff; padding: 14px 28px; border-radius: 8px; text-decoration: none; font-weight: 800; font-size: 14px; display: inline-block;"> | |
| {action_label} | |
| </a> | |
| </div> | |
| ''' | |
| safe_message = message.replace('\n', '<br>') | |
| html = f''' | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <style> | |
| body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; color: #334155; margin: 0; padding: 0; background-color: #f8fafc; }} | |
| .container {{ max-width: 600px; margin: 0 auto; background: #ffffff; border-radius: 12px; overflow: hidden; margin-top: 40px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); }} | |
| .header {{ background-color: {bg_color}; padding: 32px; text-align: center; }} | |
| .content {{ padding: 40px; }} | |
| .footer {{ padding: 24px; text-align: center; font-size: 12px; color: #94a3b8; background: #fcfcfd; border-top: 1px solid #f1f5f9; }} | |
| h1 {{ font-size: 20px; font-weight: 800; color: #0f172a; margin-top: 0; }} | |
| p {{ font-size: 15px; margin-bottom: 24px; }} | |
| </style> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <div class="header"> | |
| <img src="{logo_url}" alt="Nexus AI" height="40" style="display: block; margin: 0 auto;"> | |
| </div> | |
| <div class="content"> | |
| <h1 style="color: #0f172a;">{title}</h1> | |
| <p style="color: #334155; font-size: 15px;">{safe_message}</p> | |
| {action_button} | |
| {f'<p style="font-size: 13px; color: #64748b; font-style: italic; margin-top: 32px; border-top: 1px solid #f1f5f9; padding-top: 16px;">{footer_note}</p>' if footer_note else ''} | |
| </div> | |
| <div class="footer"> | |
| © {datetime.now().year} 3SC Nexus AI Orchestrator. All rights reserved.<br> | |
| Providing state-of-the-art logistics intelligence. | |
| </div> | |
| </div> | |
| </body> | |
| </html> | |
| ''' | |
| return html | |