Updated code and seperated in functions
This commit is contained in:
parent
ac8fe8261e
commit
f7d6fa0488
46
main.py
46
main.py
|
@ -30,6 +30,27 @@ class SMTPWebhookApp:
|
||||||
SMTP webhook server
|
SMTP webhook server
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def create_new_client(self, response):
|
||||||
|
try:
|
||||||
|
print("- - - - - CREATING CLIENT - - - - -")
|
||||||
|
mythread.client = smtplib.SMTP(host=SMTP_SERVER_HOST,
|
||||||
|
port=SMTP_SERVER_PORT)
|
||||||
|
mythread.client.starttls()
|
||||||
|
except (smtplib.SMTPException, TimeoutError) as e:
|
||||||
|
soft_return("can't connect")
|
||||||
|
response = {"status": 500,
|
||||||
|
"message": "some error: {}".format(e)}
|
||||||
|
print("response1: ", response)
|
||||||
|
|
||||||
|
def smtp_login(self, response, msg):
|
||||||
|
try:
|
||||||
|
mythread.client.login(msg["From"], FROM_MAIL_PASSWORD)
|
||||||
|
except smtplib.SMTPException as e:
|
||||||
|
soft_return(e)
|
||||||
|
response = {"status": 500,
|
||||||
|
"message": "some error: {}".format(e)}
|
||||||
|
print("response2: ", response)
|
||||||
|
|
||||||
def _send_mail(self):
|
def _send_mail(self):
|
||||||
|
|
||||||
if not cherrypy.request.headers.get('Content-Length'):
|
if not cherrypy.request.headers.get('Content-Length'):
|
||||||
|
@ -62,6 +83,7 @@ class SMTPWebhookApp:
|
||||||
msg['To'] = to_mail
|
msg['To'] = to_mail
|
||||||
|
|
||||||
c = 0
|
c = 0
|
||||||
|
response = {}
|
||||||
while (c < 2):
|
while (c < 2):
|
||||||
try:
|
try:
|
||||||
mythread.client.sendmail(msg['From'],
|
mythread.client.sendmail(msg['From'],
|
||||||
|
@ -71,24 +93,9 @@ class SMTPWebhookApp:
|
||||||
smtplib.SMTPServerDisconnected):
|
smtplib.SMTPServerDisconnected):
|
||||||
if (c == 1):
|
if (c == 1):
|
||||||
soft_return("can't connect")
|
soft_return("can't connect")
|
||||||
try:
|
|
||||||
print("- - - - - CREATING CLIENT - - - - -")
|
|
||||||
mythread.client = smtplib.SMTP(host=SMTP_SERVER_HOST,
|
|
||||||
port=SMTP_SERVER_PORT)
|
|
||||||
mythread.client.starttls()
|
|
||||||
except (smtplib.SMTPException, TimeoutError) as e:
|
|
||||||
soft_return("can't connect")
|
|
||||||
response = {"status": 500,
|
|
||||||
"message": "some error: {}".format(e)}
|
|
||||||
print("response1: ", response)
|
|
||||||
|
|
||||||
try:
|
self.create_new_client(response)
|
||||||
mythread.client.login(msg["From"], FROM_MAIL_PASSWORD)
|
self.smtp_login(response, msg)
|
||||||
except smtplib.SMTPException as e:
|
|
||||||
soft_return(e)
|
|
||||||
response = {"status": 500,
|
|
||||||
"message": "some error: {}".format(e)}
|
|
||||||
print("response2: ", response)
|
|
||||||
|
|
||||||
except smtplib.SMTPException as e:
|
except smtplib.SMTPException as e:
|
||||||
soft_return("can't send for some reason")
|
soft_return("can't send for some reason")
|
||||||
|
@ -132,10 +139,7 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
cherrypy.tree.mount(ROOT, config={})
|
cherrypy.tree.mount(ROOT, config={})
|
||||||
mythread = threading.local()
|
mythread = threading.local()
|
||||||
mythread.client = smtplib.SMTP(host=SMTP_SERVER_HOST,
|
mythread.client = None
|
||||||
port=SMTP_SERVER_PORT)
|
|
||||||
mythread.client.starttls()
|
|
||||||
|
|
||||||
if hasattr(ENGINE, "signal_handler"):
|
if hasattr(ENGINE, "signal_handler"):
|
||||||
ENGINE.signal_handler.subscribe()
|
ENGINE.signal_handler.subscribe()
|
||||||
if hasattr(ENGINE, "console_control_handler"):
|
if hasattr(ENGINE, "console_control_handler"):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user