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
|
||||
"""
|
||||
|
||||
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):
|
||||
|
||||
if not cherrypy.request.headers.get('Content-Length'):
|
||||
|
@ -62,6 +83,7 @@ class SMTPWebhookApp:
|
|||
msg['To'] = to_mail
|
||||
|
||||
c = 0
|
||||
response = {}
|
||||
while (c < 2):
|
||||
try:
|
||||
mythread.client.sendmail(msg['From'],
|
||||
|
@ -71,24 +93,9 @@ class SMTPWebhookApp:
|
|||
smtplib.SMTPServerDisconnected):
|
||||
if (c == 1):
|
||||
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:
|
||||
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)
|
||||
self.create_new_client(response)
|
||||
self.smtp_login(response, msg)
|
||||
|
||||
except smtplib.SMTPException as e:
|
||||
soft_return("can't send for some reason")
|
||||
|
@ -132,10 +139,7 @@ if __name__ == "__main__":
|
|||
|
||||
cherrypy.tree.mount(ROOT, config={})
|
||||
mythread = threading.local()
|
||||
mythread.client = smtplib.SMTP(host=SMTP_SERVER_HOST,
|
||||
port=SMTP_SERVER_PORT)
|
||||
mythread.client.starttls()
|
||||
|
||||
mythread.client = None
|
||||
if hasattr(ENGINE, "signal_handler"):
|
||||
ENGINE.signal_handler.subscribe()
|
||||
if hasattr(ENGINE, "console_control_handler"):
|
||||
|
|
Loading…
Reference in New Issue
Block a user