From 0ce2501e1a5774ae6ee8c130f7889bb10d410f27 Mon Sep 17 00:00:00 2001 From: Nathan Day <87125117+dadofsambonzuki@users.noreply.github.com> Date: Wed, 17 Jun 2026 15:59:21 +0100 Subject: [PATCH] fix: run blocking SMTP calls in a thread to avoid blocking the event loop (#3988) --- lnbits/core/services/notifications.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lnbits/core/services/notifications.py b/lnbits/core/services/notifications.py index 8860fbd6a..fba38b42f 100644 --- a/lnbits/core/services/notifications.py +++ b/lnbits/core/services/notifications.py @@ -222,12 +222,20 @@ async def send_email( msg["Subject"] = subject msg.attach(MIMEText(message, "plain")) username = username if len(username) > 0 else from_email - with smtplib.SMTP(server, port) as smtp_server: - smtp_server.starttls() - smtp_server.login(username, password) - smtp_server.sendmail(from_email, to_emails, msg.as_string()) + + def _send() -> bool: + with smtplib.SMTP(server, port) as smtp_server: + smtp_server.starttls() + smtp_server.login(username, password) + smtp_server.sendmail(from_email, to_emails, msg.as_string()) return True + try: + return await asyncio.to_thread(_send) + except Exception as e: + logger.warning(f"Sending Email failed. {e!s}") + return False + async def dispatch_webhook(payment: Payment): """