2025-01-05 21:11:56 -08:00
"""
title : Send Email
author : Josh Knapp
2025-04-15 10:19:30 -07:00
version : 0.7 .2
2025-04-15 09:29:26 -07:00
description : " Send email via SMTP to user specificed address "
2025-01-05 21:11:56 -08:00
"""
from pydantic import BaseModel , Field
2025-04-15 09:29:26 -07:00
import smtplib
from email . mime . text import MIMEText
from email . mime . multipart import MIMEMultipart
2025-01-05 21:11:56 -08:00
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-05 21:46:47 -08:00
DEBUG : bool = Field (
default = False ,
description = " Debug the output of the script, defaults to False "
)
2025-01-05 21:11:56 -08:00
def __init__ ( self ) :
self . valves = self . Valves ( )
2025-01-05 22:04:02 -08:00
print ( f " Debug Status { self . valves . DEBUG } " )
2025-01-05 21:11:56 -08: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-05 22:04:02 -08:00
if self . valves . DEBUG :
print ( " Debug activated, sending email... " )
2025-01-05 21:11:56 -08: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-05 21:46:47 -08: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-05 21:11:56 -08: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-05 21:46:47 -08: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-05 21:11:56 -08:00
server . quit ( )
return {
" success " : True ,
2025-01-05 21:46:47 -08: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-05 21:11:56 -08:00
}
except Exception as e :
2025-01-05 21:46:47 -08:00
if self . valves . DEBUG :
print ( f " Error sending email: { str ( e ) } " )
2025-01-05 21:11:56 -08:00
return {
" success " : False ,
" message " : f " Failed to send email: { str ( e ) } "
}