smtp-handling #3

Open
PeterSurda wants to merge 3 commits from cis-kuldeep/influx-smtp-gateway:smtp-handling into master
Showing only changes of commit f7d6fa0488 - Show all commits

46
main.py
View File

@ -30,6 +30,27 @@ class SMTPWebhookApp:
SMTP webhook server SMTP webhook server
""" """
def create_new_client(self, response):
Review

needs to return a value (e.g. boolean)

needs to return a value (e.g. boolean)
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):
Review

needs to return a value

needs to return a value
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)
Review

need to check for return value and skip login if connect fails

need to check for return value and skip `login` if `connect` fails
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"):