Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tool to send email via SES also updated Cookbook example for th… #1882

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cookbook/tools/aws_ses_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from phi.agent import Agent
from phi.tools.aws_ses import AWSSESTool

receiver_email = "<receiver_email>"
sender_email = "<sender_email>"
sender_name = "<sender_name>"
region_name = "<asw_region_name>"
agent = Agent(
tools=[
AWSSESTool(
receiver_email=receiver_email, sender_email=sender_email, sender_name=sender_name, region_name=region_name
)
]
)

agent.print_response("Send an email to the receiver_email with the subject hello and body hellow world.")
57 changes: 57 additions & 0 deletions phi/tools/aws_ses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import Optional
from phi.tools import Toolkit

try:
import boto3
except ImportError:
raise ImportError("boto3 is required for AWSSESTool. Please install it using `pip install boto3`.")


class AWSSESTool(Toolkit):
def __init__(
self,
receiver_email: Optional[str] = None,
sender_email: Optional[str] = None,
sender_name: Optional[str] = None,
region_name: str = "us-east-1",
):
super().__init__(name="aws_ses_tool")
self.client = boto3.client("ses", region_name=region_name)
self.receiver_email = receiver_email
self.sender_email = sender_email
self.sender_name = sender_name
self.register(self.email)

def email(self, subject: str, body: str) -> str:
"""
Args: subject: The subject of the email
body: The body of the email
"""
if not self.client:
raise Exception("AWS SES client not initialized. Please check the configuration.")
if not subject:
raise ValueError("Email subject cannot be empty.")
if not body:
raise ValueError("Email body cannot be empty.")
try:
response = self.client.send_email(
Destination={
"ToAddresses": [self.receiver_email],
},
Message={
"Body": {
"Text": {
"Charset": "UTF-8",
"Data": body,
},
},
"Subject": {
"Charset": "UTF-8",
"Data": subject,
},
},
Source=f"{self.sender_name} <{self.sender_email}>",
)
return f"Email sent successfully. Message ID: {response['MessageId']}"
except Exception as e:
raise Exception(f"Failed to send email: {e}")