Spaces:
Runtime error
Runtime error
Download email_utils.py from kalyan9/HealthGuard-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/kalyan9/HealthGuard-AI/resolve/main/email_utils.py
- Command line
-
hf download hf://spaces/kalyan9/HealthGuard-AI/email_utils.py
-
curl -L -o email_utils.py https://huggingface.co/spaces/kalyan9/HealthGuard-AI/resolve/main/email_utils.py
1.47 kB
| import os | |
| import smtplib | |
| from email.message import EmailMessage | |
| EMAIL_USER = os.getenv("EMAIL_USER") | |
| # Support EMAIL_PASSWORD as either plain text or base64-encoded string. | |
| # If it's base64-encoded, decode it; otherwise, use the raw value. | |
| _raw_email_password = os.getenv("EMAIL_PASSWORD") | |
| EMAIL_PASSWORD = None | |
| if _raw_email_password: | |
| # Try base64 decode, fall back to raw string if decode fails or is nonsense | |
| try: | |
| import base64 as _b64 | |
| decoded = _b64.b64decode(_raw_email_password).decode() | |
| # Basic sanity: decoded password should be non-empty | |
| if decoded: | |
| EMAIL_PASSWORD = decoded | |
| else: | |
| EMAIL_PASSWORD = _raw_email_password | |
| except Exception: | |
| EMAIL_PASSWORD = _raw_email_password | |
| def send_email(to_email, subject, body): | |
| """Send an email using SMTP_SSL (Gmail). | |
| Reads EMAIL_USER and EMAIL_PASSWORD from environment variables. | |
| Raises RuntimeError if credentials are not set. | |
| """ | |
| if not EMAIL_USER or not EMAIL_PASSWORD: | |
| raise RuntimeError("Email credentials not configured in environment variables") | |
| msg = EmailMessage() | |
| msg.set_content(body) | |
| msg["Subject"] = subject | |
| msg["From"] = EMAIL_USER | |
| msg["To"] = to_email | |
| # Use SMTP_SSL for a secure connection | |
| with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp: | |
| smtp.login(EMAIL_USER, EMAIL_PASSWORD) | |
| smtp.send_message(msg) | |