File size: 2,270 Bytes
9118743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import re
from typing import Dict

from mailjet_rest import Client
from agents import Agent, function_tool


def is_valid_email(email: str) -> bool:
    """Validate email format using regular expressions."""
    if not email or not email.strip():
        return False
    email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    return re.match(email_pattern, email.strip()) is not None


# Global variable to store recipient email
_recipient_email = None


def set_recipient_email(email: str):
    """Set the recipient email address."""
    global _recipient_email
    _recipient_email = email if email and is_valid_email(email) else None

@function_tool
def send_email(subject: str, html_body: str) -> Dict[str, str]:
    """ Send an email with the given subject and HTML body """
    global _recipient_email

    # If no valid recipient email is set, return without sending
    if not _recipient_email:
        return {"status": "skipped", "message": "No recipient email provided"}

    mailjet = Client(
        auth=(os.environ.get('MAILJET_API_KEY'), os.environ.get('MAILJET_SECRET_KEY')),
        version='v3.1'
    )

    from_email = "[email protected]"  # put your verified sender here
    to_email = _recipient_email
    
    data = {
        'Messages': [
            {
                "From": {
                    "Email": from_email,
                    "Name": "Email Agent"
                },
                "To": [
                    {
                        "Email": to_email
                    }
                ],
                "Subject": subject,
                "HTMLPart": html_body
            }
        ]
    }
    
    result = mailjet.send.create(data=data)
    print("Email response", result.status_code)
    print("Email response body", result.json())
    return {"status": "success"}

INSTRUCTIONS = """You are able to send a nicely formatted HTML email based on a detailed report.
You will be provided with a detailed report. You should use your tool to send one email, providing the 
report converted into clean, well presented HTML with an appropriate subject line."""

email_agent = Agent(
    name="Email agent",
    instructions=INSTRUCTIONS,
    tools=[send_email],
    model="gpt-4o-mini",
)