File size: 1,086 Bytes
8ed954c
82de0b8
8ed954c
82de0b8
8ed954c
 
 
 
 
 
 
 
 
 
5fadea9
 
8ed954c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import threading
import resend
from src.core.logger import get_logger

logger = get_logger(__name__)

_send_lock = threading.Lock()


def send_email_report(subject: str, html_content: str, recipient: str, api_key: str):
    """Send an HTML email via Resend.

    Uses a lock around ``resend.api_key`` assignment to prevent race
    conditions when multiple emails are dispatched concurrently.
    """
    if not api_key:
        logger.warning("No API key for %s – email skipped", recipient)
        return None

    with _send_lock:
        try:
            resend.api_key = api_key

            params = {
                "from": "PrimoGreedy <onboarding@resend.dev>",
                "to": [recipient],
                "subject": subject,
                "html": html_content,
            }

            result = resend.Emails.send(params)
            logger.info("Email sent to %s (ID: %s)", recipient, result.get("id"))
            return result

        except Exception as exc:
            logger.error("Failed to send email to %s: %s", recipient, exc)
            return None