from typing import Any import httpx class SendgridClient: api_url = "https://api.sendgrid.com/v3/mail/send" def __init__(self, api_key: str, timeout: int = 10) -> None: self.api_key = api_key self.timeout = timeout def send_email( self, to_email: str, subject: str, text_content: str, html_content: str, from_email: str, from_email_display_name: str, reply_to: str, categories: list[str] | None = None, custom_args: dict[str, Any] | None = None, ) -> None: content: list[dict[str, str]] = [] if text_content: content.append({"type": "text/plain", "value": text_content}) if html_content: content.append({"type": "text/html", "value": html_content}) if not content: content.append({"type": "text/plain", "value": "No content provided"}) payload: dict[str, Any] = { "from": {"email": from_email, "name": from_email_display_name}, "personalizations": [{"to": [{"email": to_email}]}], "reply_to": {"email": reply_to}, "subject": subject, "content": content, } if categories: payload["categories"] = categories if custom_args: payload["custom_args"] = custom_args response = httpx.post( self.api_url, headers={"Authorization": f"Bearer {self.api_key}"}, json=payload, timeout=self.timeout, ) response.raise_for_status()