[docs]classEmail(ToolBase):"""A toy tool to send emails via a given SMTP server."""def__init__(self,*args,email_from:str,smtp_host:str,smtp_pass:str,**kwargs):""" :param email_from: the email address to send email from :param smtp_host: a string like mailserv.zhu.codes:465 :param smtp_pass: the password for the email account on the SMTP server """super().__init__(*args,**kwargs)self.email_from=email_fromself.smtp_host=smtp_hostself.smtp_pass=smtp_pass
[docs]@ai_function()defsend_email(self,to:str,subject:str,body:str):"""Send an email to the given address."""msg=EmailMessage()msg.set_content(body)# me == the sender's email address# you == the recipient's email addressmsg["Subject"]=subjectmsg["From"]=self.email_frommsg["To"]=to.strip("<>")msg["Date"]=datetime.datetime.now(tz=datetime.timezone.utc).isoformat()# Send the message via our own SMTP server.s=smtplib.SMTP_SSL(self.smtp_host)s.login(self.email_from,self.smtp_pass)s.send_message(msg)s.quit()return"Email sent!"