Spaces:
Running on T4
Running on T4
File size: 1,102 Bytes
2756f0f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | """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()
@lru_cache
def get_email_client() -> EmailClient:
return EmailClient(get_settings())
|