""" title: Send Email author: Josh Knapp version: 0.4.0 description="Send email via SMTP to user specificed address" """ import subprocess, sys from pydantic import BaseModel, Field # Try to import required packages, 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" ) DEBUG: bool = Field( default=False, description="Debug the output of the script, defaults to False" ) def __init__(self): self.valves = self.Valves() print(f"Debug Status {self.valves.DEBUG}") 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 """ if self.valves.DEBUG: print("Debug activated, sending email...") 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 + "\n\n This email was sent via OpenWebUI-SendEmail - https://repo.anhonesthost.net/OpenWebUI-Tools/OpenWebUI-SendEmail", '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() if self.valves.DEBUG: print(f"Sending email to {to_address} with subject: {subject}") print(f"Message content: {message}") print(f"Using server: {self.valves.MAIL_SERVER_ADDR}:{self.valves.MAIL_SERVER_PORT}") print(f"Using SSL: {self.valves.MAIL_SERVER_SSL}") print(f"Using credentials: {self.valves.EMAIL_ADDRESS}") recipients = [to_address] if self.valves.CC_EMAIL: recipients.append(self.valves.CC_EMAIL) server.sendmail(self.valves.EMAIL_ADDRESS, recipients, text) server.quit() return { "success": True, "message": f"Email sent successfully to {to_address} with CC to {self.valves.CC_EMAIL if self.valves.CC_EMAIL else 'no one'}" } except Exception as e: if self.valves.DEBUG: print(f"Error sending email: {str(e)}") return { "success": False, "message": f"Failed to send email: {str(e)}" }