2025-01-06 05:11:56 +00:00
"""
title : Send Email
author : Josh Knapp
2025-01-06 06:04:02 +00:00
version : 0.4 .0
2025-01-06 05:11:56 +00:00
description = " Send email via SMTP to user specificed address "
"""
import subprocess , sys
from pydantic import BaseModel , Field
2025-01-06 06:04:02 +00:00
# Try to import required packages, install if not present
2025-01-06 05:11:56 +00:00
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 "
)
2025-01-06 05:46:47 +00:00
DEBUG : bool = Field (
default = False ,
description = " Debug the output of the script, defaults to False "
)
2025-01-06 05:11:56 +00:00
def __init__ ( self ) :
self . valves = self . Valves ( )
2025-01-06 06:04:02 +00:00
print ( f " Debug Status { self . valves . DEBUG } " )
2025-01-06 05:11:56 +00:00
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
"""
2025-01-06 06:04:02 +00:00
if self . valves . DEBUG :
print ( " Debug activated, sending email... " )
2025-01-06 05:11:56 +00:00
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
2025-01-06 05:46:47 +00:00
msg . attach ( MIMEText ( message + " \n \n This email was sent via OpenWebUI-SendEmail - https://repo.anhonesthost.net/OpenWebUI-Tools/OpenWebUI-SendEmail " , ' plain ' ) )
2025-01-06 05:11:56 +00:00
# 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
2025-01-06 05:46:47 +00:00
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 )
2025-01-06 05:11:56 +00:00
server . quit ( )
return {
" success " : True ,
2025-01-06 05:46:47 +00:00
" message " : f " Email sent successfully to { to_address } with CC to { self . valves . CC_EMAIL if self . valves . CC_EMAIL else ' no one ' } "
2025-01-06 05:11:56 +00:00
}
except Exception as e :
2025-01-06 05:46:47 +00:00
if self . valves . DEBUG :
print ( f " Error sending email: { str ( e ) } " )
2025-01-06 05:11:56 +00:00
return {
" success " : False ,
" message " : f " Failed to send email: { str ( e ) } "
}