smtp-handling #3
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):
|
||||
PeterSurda
commented
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):
|
||||
|
||||
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)
|
||||
PeterSurda
commented
need to check for return value and skip need to check for return value and skip `login` if `connect` fails
|
||||
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
needs to return a value (e.g. boolean)