Make SMTP connections thread safe
This commit is contained in:
parent
107b9be003
commit
2e85ec653f
@ -31,31 +31,10 @@ else:
|
|||||||
from _thread import start_new_thread
|
from _thread import start_new_thread
|
||||||
|
|
||||||
|
|
||||||
class SMTP(object):
|
class SMTPConnection(object):
|
||||||
|
|
||||||
def __init__(self, isso):
|
def __init__(self, conf):
|
||||||
|
self.conf = conf
|
||||||
self.isso = isso
|
|
||||||
self.conf = isso.conf.section("smtp")
|
|
||||||
|
|
||||||
# test SMTP connectivity
|
|
||||||
try:
|
|
||||||
with self:
|
|
||||||
logger.info("connected to SMTP server")
|
|
||||||
except (socket.error, smtplib.SMTPException):
|
|
||||||
logger.exception("unable to connect to SMTP server")
|
|
||||||
|
|
||||||
if uwsgi:
|
|
||||||
def spooler(args):
|
|
||||||
try:
|
|
||||||
self._sendmail(args[b"subject"].decode("utf-8"),
|
|
||||||
args["body"].decode("utf-8"))
|
|
||||||
except smtplib.SMTPConnectError:
|
|
||||||
return uwsgi.SPOOL_RETRY
|
|
||||||
else:
|
|
||||||
return uwsgi.SPOOL_OK
|
|
||||||
|
|
||||||
uwsgi.spooler = spooler
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
klass = (smtplib.SMTP_SSL if self.conf.get(
|
klass = (smtplib.SMTP_SSL if self.conf.get(
|
||||||
@ -85,6 +64,32 @@ class SMTP(object):
|
|||||||
def __exit__(self, exc_type, exc_value, traceback):
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
self.client.quit()
|
self.client.quit()
|
||||||
|
|
||||||
|
class SMTP(object):
|
||||||
|
|
||||||
|
def __init__(self, isso):
|
||||||
|
|
||||||
|
self.isso = isso
|
||||||
|
self.conf = isso.conf.section("smtp")
|
||||||
|
|
||||||
|
# test SMTP connectivity
|
||||||
|
try:
|
||||||
|
with SMTPConnection(self.conf):
|
||||||
|
logger.info("connected to SMTP server")
|
||||||
|
except (socket.error, smtplib.SMTPException):
|
||||||
|
logger.exception("unable to connect to SMTP server")
|
||||||
|
|
||||||
|
if uwsgi:
|
||||||
|
def spooler(args):
|
||||||
|
try:
|
||||||
|
self._sendmail(args[b"subject"].decode("utf-8"),
|
||||||
|
args["body"].decode("utf-8"))
|
||||||
|
except smtplib.SMTPConnectError:
|
||||||
|
return uwsgi.SPOOL_RETRY
|
||||||
|
else:
|
||||||
|
return uwsgi.SPOOL_OK
|
||||||
|
|
||||||
|
uwsgi.spooler = spooler
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
yield "comments.new:after-save", self.notify
|
yield "comments.new:after-save", self.notify
|
||||||
|
|
||||||
@ -153,7 +158,7 @@ class SMTP(object):
|
|||||||
msg['Date'] = formatdate(localtime=True)
|
msg['Date'] = formatdate(localtime=True)
|
||||||
msg['Subject'] = Header(subject, 'utf-8')
|
msg['Subject'] = Header(subject, 'utf-8')
|
||||||
|
|
||||||
with self as con:
|
with SMTPConnection(self.conf) as con:
|
||||||
con.sendmail(from_addr, to_addr, msg.as_string())
|
con.sendmail(from_addr, to_addr, msg.as_string())
|
||||||
|
|
||||||
def _retry(self, subject, body, to):
|
def _retry(self, subject, body, to):
|
||||||
|
Loading…
Reference in New Issue
Block a user