Commit 6f00a5fc authored by ale's avatar ale
Browse files

Set timeouts for IMAP and SMTP connections

parent 59f465c3
......@@ -9,6 +9,10 @@ from email.utils import formatdate
from email.mime.text import MIMEText
IMAP_TIMEOUT = 30
SMTP_TIMEOUT = 30
class _SSLContext(ssl.SSLContext):
"""A SSLContext that forces a server_name for SNI."""
......@@ -28,6 +32,7 @@ def imap_conn(server_name, server_addr=None):
ssl_ctx.load_default_certs()
ssl_ctx.set_server_name(server_name)
conn = imaplib.IMAP4_SSL(server_addr, ssl_context=ssl_ctx)
conn.socket().settimeout(IMAP_TIMEOUT)
try:
yield conn
finally:
......@@ -73,7 +78,7 @@ def smtp_conn(server_name, server_addr=None, port=587):
ssl_ctx = _SSLContext(ssl.PROTOCOL_TLSv1_2)
ssl_ctx.load_default_certs()
ssl_ctx.set_server_name(server_name)
conn = smtplib.SMTP(server_addr, port)
conn = smtplib.SMTP(server_addr, port, timeout=SMTP_TIMEOUT)
conn.ehlo()
conn.starttls(context=ssl_ctx)
conn.ehlo()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment