Research_Email_AI-Agent / email_agent.py
futuretechnocratassociation's picture
Update email_agent.py
da8df91 verified
Raw
History Blame Contribute Delete
3.42 kB
import os
from typing import Dict
import sendgrid
from sendgrid.helpers.mail import Email, Mail, Content, To
from agents import Agent, function_tool, OpenAIChatCompletionsModel
from openai import AsyncOpenAI
import os
from dotenv import load_dotenv
load_dotenv(override=True)
model = "gemini-2.0-flash"
GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/"
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
client = AsyncOpenAI(base_url=GEMINI_BASE_URL,api_key=GEMINI_API_KEY)
custom_model = OpenAIChatCompletionsModel(model=model,openai_client=client)
@function_tool
def send_email(subject: str, html_body: str,email: str) -> Dict[str, str]:
""" Send an email with the given subject and HTML body """
sg = sendgrid.SendGridAPIClient('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')
from_email = Email("futuretechnocratassociation@gmail.com") # put your verified sender here
to_email = To(email) # put your recipient here
content = Content("text/html", html_body)
mail = Mail(from_email, to_email, subject, content).get()
response = sg.client.mail.send.post(request_body=mail)
print("Email response", response.status_code)
return {"status": "success", "note": "Do not send any further emails"}
# email_sent = False
# @function_tool
# def send_email(subject: str, html_body: str, email: str) -> Dict[str, str]:
# global email_sent
# if email_sent:
# print("Email already sent, skipping...")
# return {"status": "skipped"}
# try:
# sg = sendgrid.SendGridAPIClient('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')
# from_email = Email("futuretechnocratassociation@gmail.com")
# to_email = To(email)
# content = Content("text/html", html_body)
# mail = Mail(from_email, to_email, subject, content).get()
# response = sg.client.mail.send.post(request_body=mail)
# if response.status_code in [200, 202]: # success
# email_sent = True # ✅ only mark after successful send
# print("Email response", response.status_code)
# return {"status": "success"}
# else:
# print("Failed with status:", response.status_code)
# return {"status": "failed"}
# except Exception as e:
# print("Error sending email:", str(e))
# return {"status": "error"}
INSTRUCTIONS = """You are able to send a nicely formatted colourful 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. You will also get an email address along with the report—send only to that email.
If you receive the message:
`{"status": "success", "note": "Do not send any further emails"}`
then do not send any further emails.
Finally, call the tool exactly once in this format:
send_email(subject, html_body, email)
You MUST call the tool exactly once.
Never call it more than once, even if asked.
If you already called it, or if you received the above success message, do nothing else.
Do not output anything else. Only send the formatted email using the tool.
"""
email_agent = Agent(
name="Email agent",
instructions=INSTRUCTIONS,
tools=[send_email],
model=custom_model,
)