Added Send mail functionality & Dockerized application #1
25
main.py
25
main.py
|
@ -26,8 +26,7 @@ class SMTPWebhookApp:
|
||||||
def _send_mail(self):
|
def _send_mail(self):
|
||||||
|
|
||||||
PeterSurda marked this conversation as resolved
Outdated
|
|||||||
if not cherrypy.request.headers.get('Content-Length'):
|
if not cherrypy.request.headers.get('Content-Length'):
|
||||||
logging.error("To: {}, error: Invalid content length.".format(
|
logging.error("error: Invalid content length.")
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
port 587 port 587
|
|||||||
TO_MAIL))
|
|
||||||
return {"status": 400, "message": "Invalid content length."}
|
return {"status": 400, "message": "Invalid content length."}
|
||||||
|
|
||||||
cl = cherrypy.request.headers['Content-Length']
|
cl = cherrypy.request.headers['Content-Length']
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
this whole class we don't need this whole class we don't need
|
|||||||
|
@ -35,30 +34,33 @@ class SMTPWebhookApp:
|
||||||
req_body = json.loads(rawbody)
|
req_body = json.loads(rawbody)
|
||||||
|
|
||||||
if not req_body.get('subject') or req_body.get('subject') == '':
|
if not req_body.get('subject') or req_body.get('subject') == '':
|
||||||
logging.error("To: {}, error: body field is required.".format(
|
logging.error("error: body field is required.")
|
||||||
TO_MAIL))
|
|
||||||
return {"status": 400, "message": "subject field is required."}
|
return {"status": 400, "message": "subject field is required."}
|
||||||
|
|
||||||
if not req_body.get('body') or req_body.get('body') == '':
|
if not req_body.get('body') or req_body.get('body') == '':
|
||||||
logging.error("To: {}, error: body field is required.".format(
|
logging.error("error: body field is required.")
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
ideally an exception per line rather than together, then you can also use more specific exceptions. ideally an exception per line rather than together, then you can also use more specific exceptions.
|
|||||||
TO_MAIL))
|
|
||||||
return {"status": 400, "message": "body field is required."}
|
return {"status": 400, "message": "body field is required."}
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
- return an error code (in addition to error text) if there's an error
- add a small delay if there's an error before returning, say 0.2s, and try to avoid `sleep`, instead look for a way to do this asynchronously
|
|||||||
|
|
||||||
|
if not req_body.get('to_mail') or req_body.get('to_mail') == '':
|
||||||
|
logging.error("error: to_mail field is required.")
|
||||||
|
return {"status": 400, "message": "to_mail field is required."}
|
||||||
|
|
||||||
subject = req_body['subject']
|
subject = req_body['subject']
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
docstring docstring
|
|||||||
body = req_body['body']
|
body = req_body['body']
|
||||||
|
to_mail = req_body['to_mail']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`except (SMTPConnectionError, TimeoutError) as e:`
|
|||||||
client = smtplib.SMTP(host=SMTP_SERVER_HOST, port=SMTP_SERVER_PORT)
|
client = smtplib.SMTP(host=SMTP_SERVER_HOST, port=SMTP_SERVER_PORT)
|
||||||
except (smtplib.SMTPConnectionError, TimeoutError) as e:
|
except (smtplib.SMTPConnectionError, TimeoutError) as e:
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
logging.error("To: {}, error: {}".format(TO_MAIL, e))
|
logging.error("To: {}, error: {}".format(to_mail, e))
|
||||||
return {"status": 400, "message": "SMTP client error: {}.".format(
|
return {"status": 400, "message": "SMTP client error: {}.".format(
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`SERVER_PORT` should default to `587` and be an integer.
|
|||||||
e)}
|
e)}
|
||||||
|
|
||||||
msg = MIMEText(body, 'plain', 'utf-8')
|
msg = MIMEText(body, 'plain', 'utf-8')
|
||||||
msg['Subject'] = Header(subject, 'utf-8')
|
msg['Subject'] = Header(subject, 'utf-8')
|
||||||
msg['From'] = FROM_MAIL
|
msg['From'] = FROM_MAIL
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`except KeyError`
|
|||||||
msg['To'] = TO_MAIL
|
msg['To'] = to_mail
|
||||||
try:
|
try:
|
||||||
client.ehlo()
|
client.ehlo()
|
||||||
client.starttls()
|
client.starttls()
|
||||||
|
@ -66,10 +68,10 @@ class SMTPWebhookApp:
|
||||||
client.login(msg["From"], FROM_MAIL_PASSWORD)
|
client.login(msg["From"], FROM_MAIL_PASSWORD)
|
||||||
client.sendmail(msg['From'], msg['To'], msg.as_string())
|
client.sendmail(msg['From'], msg['To'], msg.as_string())
|
||||||
response = {"status": 200, "message": "mail sent successfully"}
|
response = {"status": 200, "message": "mail sent successfully"}
|
||||||
logging.info("To: {}, mail sent successfully".format(TO_MAIL))
|
logging.info("To: {}, mail sent successfully".format(to_mail))
|
||||||
except smtplib.SMTPException as e:
|
except smtplib.SMTPException as e:
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
merge with previous merge with previous `try`/`except`
|
|||||||
logging.error("To: {}, error: {}".format(TO_MAIL, e))
|
logging.error("To: {}, error: {}".format(to_mail, e))
|
||||||
response = {"status": 500, "message": "some error: {}".format(e)}
|
response = {"status": 500, "message": "some error: {}".format(e)}
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`quit()` should be in `finally:`
|
|||||||
finally:
|
finally:
|
||||||
client.quit()
|
client.quit()
|
||||||
|
@ -95,12 +97,11 @@ CHERRYPY_SERVER_PORT = 8081
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
remove remove
|
|||||||
SMTP_SERVER_HOST = os.environ["SMTP_SERVER_HOST"]
|
SMTP_SERVER_HOST = os.environ["SMTP_SERVER_HOST"]
|
||||||
TO_MAIL = os.environ["TO_MAIL"]
|
|
||||||
FROM_MAIL = os.environ["FROM_MAIL"]
|
FROM_MAIL = os.environ["FROM_MAIL"]
|
||||||
FROM_MAIL_PASSWORD = os.environ["FROM_MAIL_PASSWORD"]
|
FROM_MAIL_PASSWORD = os.environ["FROM_MAIL_PASSWORD"]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise KeyError("Please check missing environment variables: "
|
raise KeyError("Please check missing environment variables: "
|
||||||
"SMTP_SERVER_HOST, TO_MAIL, FROM_MAIL, "
|
"SMTP_SERVER_HOST, FROM_MAIL, "
|
||||||
"FROM_MAIL_PASSWORD")
|
"FROM_MAIL_PASSWORD")
|
||||||
|
|
||||||
cherrypy.server.socket_host = CHERRYPY_SERVER_HOST
|
cherrypy.server.socket_host = CHERRYPY_SERVER_HOST
|
||||||
|
|
Loading…
Reference in New Issue
Block a user
debug we don't need