Spaces:
Running on T4
Running on T4
Download backend/app/integrations/email.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/integrations/email.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/app/integrations/email.py
-
curl -L -o email.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/integrations/email.py
1.1 kB
| """Transactional email through the Resend HTTP API. Without an API key (dev, test) messages are only logged.""" | |
| import logging | |
| from functools import lru_cache | |
| import httpx | |
| from app.core.config import Settings, get_settings | |
| log = logging.getLogger(__name__) | |
| RESEND_URL = "https://api.resend.com/emails" | |
| class EmailClient: | |
| def __init__(self, settings: Settings): | |
| self.api_key = settings.resend_api_key | |
| self.sender = settings.email_from | |
| async def send(self, to: str, subject: str, text: str) -> None: | |
| if not self.api_key: | |
| log.info("email not sent (no RESEND_API_KEY): to=%s subject=%s", to, subject) | |
| return | |
| async with httpx.AsyncClient(timeout=10) as client: | |
| response = await client.post( | |
| RESEND_URL, | |
| headers={"Authorization": f"Bearer {self.api_key}"}, | |
| json={"from": self.sender, "to": [to], "subject": subject, "text": text}, | |
| ) | |
| response.raise_for_status() | |
| def get_email_client() -> EmailClient: | |
| return EmailClient(get_settings()) | |