github-actions[bot]
Automated Deploy: 0d5b759d8eca219a25b3ab717925e73a66bbf4a4
2756f0f
Raw History Blame Contribute Delete
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()
@lru_cache
def get_email_client() -> EmailClient:
return EmailClient(get_settings())