""" 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)}" }