From 2700669489a3c4fb33b6114e3d26b6fea4829f49 Mon Sep 17 00:00:00 2001 From: Josh Knapp Date: Sun, 5 Jan 2025 21:11:56 -0800 Subject: [PATCH] first push of tool --- README.md | 6 ++- open-webui-send-email.py | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 open-webui-send-email.py diff --git a/README.md b/README.md index 6c659b9..1ad34a1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # OpenWebUI-SendEmail -OpenWebUI Send Email to User specified email address. \ No newline at end of file +OpenWebUI Send Email to User specified email address. + +``` +You have access to a tool called "Send Email" that allows you to send an email to a user supplied email address. The tool only supports plain text for subject and messages. If the user does not provide a valid email address in the request, please ask them for it. To use the tool, you need to provide the 'to_address' as a string, the 'subject' as a string, and the 'message' as a string. The tool will return a dictionary variable, with it either showing the success being true or false. If success was false, it will return an error, which you should show to the user. Only use this tool when handling sending emails. +``` \ No newline at end of file diff --git a/open-webui-send-email.py b/open-webui-send-email.py new file mode 100644 index 0000000..ac5fc84 --- /dev/null +++ b/open-webui-send-email.py @@ -0,0 +1,113 @@ +""" +title: Send Email +author: Josh Knapp +version: 0.1.0 +description="Send email via SMTP to user specificed address" +""" + +import subprocess, sys +from pydantic import BaseModel, Field + + +# Try to import boto3, install if not present +try: + import smtplib + from email.mime.text import MIMEText + from email.mime.multipart import MIMEMultipart +except ImportError: + print("Packages not found. Attempting to install...") + try: + subprocess.check_call([sys.executable, "-m", "pip", "install", "smtplib"]) + subprocess.check_call([sys.executable, "-m", "pip", "install", "email"]) + + print("Packages installed successfully") + except subprocess.CalledProcessError as e: + print(f"Failed to install packages: {str(e)}") + + +class Tools: + class Valves(BaseModel): + MAIL_SERVER_ADDR: str = Field( + default="", + description="SMTP server address for sending emails", + ) + MAIL_SERVER_PORT: str = Field( + default="465", + description="SMTP server port for sending emails, defaults to 465", + ) + MAIL_SERVER_SSL: bool = Field( + default=True, + description="Bool to use SSL for SMTP, defaults to True, use TLS for secure email transmission" + ) + EMAIL_ADDRESS: str = Field( + default="", + description="Email address for sending emails" + ) + EMAIL_PASSWORD: str = Field( + default="", + description="Password for authenticating with the SMTP server" + ) + CC_EMAIL: str = Field( + default="", + description="Email address to CC on all outgoing emails" + ) + + def __init__(self): + self.valves = self.Valves() + pass + + def send_email(self, to_address: str, subject: str, message: str) -> dict: + """ + Send an email using configured SMTP settings + + Args: + to_address (str): Recipient email address + subject (str): Email subject line + message (str): Email body content + + Returns: + dict: Response containing success status and message + """ + try: + # Create message container + msg = MIMEMultipart() + msg['From'] = self.valves.EMAIL_ADDRESS + msg['To'] = to_address + if self.valves.CC_EMAIL: + msg['Cc'] = self.valves.CC_EMAIL + msg['Subject'] = subject + + # Add body to email + msg.attach(MIMEText(message, 'plain')) + + # Create SMTP connection + if self.valves.MAIL_SERVER_SSL: + server = smtplib.SMTP_SSL( + self.valves.MAIL_SERVER_ADDR, + int(self.valves.MAIL_SERVER_PORT) + ) + else: + server = smtplib.SMTP( + self.valves.MAIL_SERVER_ADDR, + int(self.valves.MAIL_SERVER_PORT) + ) + + # Login to server + server.login(self.valves.EMAIL_ADDRESS, self.valves.EMAIL_PASSWORD) + + # Send email + text = msg.as_string() + "\n\n This email was sent via OpenWebUI-SendEmail" + server.sendmail(self.valves.EMAIL_ADDRESS, to_address, text) + server.quit() + + return { + "success": True, + "message": f"Email sent successfully to {to_address}" + } + + except Exception as e: + return { + "success": False, + "message": f"Failed to send email: {str(e)}" + } +