mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-14 19:16:01 +08:00
fix: add timeout to SMTPClient to prevent worker blocking (#4352)
This commit is contained in:
parent
98140ae5d9
commit
2eb468f885
@ -1,3 +1,4 @@
|
|||||||
|
import logging
|
||||||
import smtplib
|
import smtplib
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
@ -13,15 +14,30 @@ class SMTPClient:
|
|||||||
self._use_tls = use_tls
|
self._use_tls = use_tls
|
||||||
|
|
||||||
def send(self, mail: dict):
|
def send(self, mail: dict):
|
||||||
smtp = smtplib.SMTP(self.server, self.port)
|
smtp = None
|
||||||
if self._use_tls:
|
try:
|
||||||
smtp.starttls()
|
smtp = smtplib.SMTP(self.server, self.port, timeout=10)
|
||||||
if self.username and self.password:
|
if self._use_tls:
|
||||||
smtp.login(self.username, self.password)
|
smtp.starttls()
|
||||||
msg = MIMEMultipart()
|
if self.username and self.password:
|
||||||
msg['Subject'] = mail['subject']
|
smtp.login(self.username, self.password)
|
||||||
msg['From'] = self._from
|
|
||||||
msg['To'] = mail['to']
|
msg = MIMEMultipart()
|
||||||
msg.attach(MIMEText(mail['html'], 'html'))
|
msg['Subject'] = mail['subject']
|
||||||
smtp.sendmail(self.username, mail['to'], msg.as_string())
|
msg['From'] = self._from
|
||||||
smtp.quit()
|
msg['To'] = mail['to']
|
||||||
|
msg.attach(MIMEText(mail['html'], 'html'))
|
||||||
|
|
||||||
|
smtp.sendmail(self._from, mail['to'], msg.as_string())
|
||||||
|
except smtplib.SMTPException as e:
|
||||||
|
logging.error(f"SMTP error occurred: {str(e)}")
|
||||||
|
raise
|
||||||
|
except TimeoutError as e:
|
||||||
|
logging.error(f"Timeout occurred while sending email: {str(e)}")
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Unexpected error occurred while sending email: {str(e)}")
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
if smtp:
|
||||||
|
smtp.quit()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user