File size: 1,426 Bytes
447ebeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
This is the litellm x resend email integration

https://resend.com/docs/api-reference/emails/send-email
"""

import os
from typing import List

from litellm._logging import verbose_logger
from litellm.llms.custom_httpx.http_handler import (
    get_async_httpx_client,
    httpxSpecialProvider,
)

from .base_email import BaseEmailLogger

RESEND_API_ENDPOINT = "https://api.resend.com/emails"


class ResendEmailLogger(BaseEmailLogger):
    def __init__(self):
        self.async_httpx_client = get_async_httpx_client(
            llm_provider=httpxSpecialProvider.LoggingCallback
        )
        self.resend_api_key = os.getenv("RESEND_API_KEY")

    async def send_email(
        self,
        from_email: str,
        to_email: List[str],
        subject: str,
        html_body: str,
    ):
        verbose_logger.debug(
            f"Sending email from {from_email} to {to_email} with subject {subject}"
        )
        response = await self.async_httpx_client.post(
            url=RESEND_API_ENDPOINT,
            json={
                "from": from_email,
                "to": to_email,
                "subject": subject,
                "html": html_body,
            },
            headers={"Authorization": f"Bearer {self.resend_api_key}"},
        )
        verbose_logger.debug(
            f"Email sent with status code {response.status_code}. Got response: {response.json()}"
        )
        return