Changed username and Added error handling & logging
buildbot/travis_bionic Build done. Details

This commit is contained in:
kuldeep.k@cisinlabs.com 2022-02-15 22:18:24 +05:30
parent ae2b959c9e
commit 4cca0e8aa9
2 changed files with 69 additions and 22 deletions

View File

@ -14,8 +14,8 @@ COPY ./requirements.txt .
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
# add user # add user
RUN adduser --disabled-password --gecos '' newuser RUN adduser --disabled-password --gecos '' service
USER newuser USER service
# copy project # copy project
COPY . . COPY . .

87
main.py
View File

@ -7,10 +7,15 @@ import os
import json import json
import smtplib import smtplib
import sys import sys
import time
from email.header import Header from email.header import Header
from email.mime.text import MIMEText from email.mime.text import MIMEText
import cherrypy import cherrypy
import logging
logging.basicConfig(filename='app.log', filemode='w',
format='%(name)s - %(levelname)s - %(message)s')
class SMTPWebhookApp: class SMTPWebhookApp:
@ -19,34 +24,71 @@ class SMTPWebhookApp:
""" """
def _send_mail(self): def _send_mail(self):
try:
cl = cherrypy.request.headers['Content-Length']
rawbody = cherrypy.request.body.read(int(cl))
req_body = json.loads(rawbody)
subject = req_body['subject']
body = req_body['body']
client = smtplib.SMTP(host=SMTP_SERVER_HOST, port=SMTP_SERVER_PORT)
msg = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = FROM_MAIL
msg['To'] = TO_MAIL
if not cherrypy.request.headers.get('Content-Length'):
logging.error("To: {}, error: Invalid content length.".format(
TO_MAIL))
return {"status": 400, "message": "Invalid content length."}
cl = cherrypy.request.headers['Content-Length']
rawbody = cherrypy.request.body.read(int(cl))
req_body = json.loads(rawbody)
if not req_body.get('subject') or req_body.get('subject') == '':
logging.error("To: {}, error: body field is required.".format(
TO_MAIL))
return {"status": 400, "message": "subject field is required."}
if not req_body.get('body') or req_body.get('body') == '':
logging.error("To: {}, error: body field is required.".format(
TO_MAIL))
return {"status": 400, "message": "body field is required."}
subject = req_body['subject']
body = req_body['body']
try:
client = smtplib.SMTP(host=SMTP_SERVER_HOST, port=SMTP_SERVER_PORT)
except Exception as e:
time.sleep(0.2)
logging.error("To: {}, error: {}".format(TO_MAIL, e))
return {"status": 400, "message": "SMTP client error: {}.".format(
e)}
msg = MIMEText(body, 'plain', 'utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = FROM_MAIL
msg['To'] = TO_MAIL
try:
client.ehlo() client.ehlo()
client.starttls() client.starttls()
client.ehlo() client.ehlo()
client.login(msg["From"], FROM_MAIL_PASSWORD) client.login(msg["From"], FROM_MAIL_PASSWORD)
except Exception as e:
time.sleep(0.2)
logging.error("To: {}, error: {}".format(msg['To'], e))
return {"status": 500, "message": "some error in from mail "
"login: {}".format(e)}
try:
client.sendmail(msg['From'], msg['To'], msg.as_string()) client.sendmail(msg['From'], msg['To'], msg.as_string())
client.quit() client.quit()
return "mail sent successfully" logging.info("To: {}, mail sent successfully".format(TO_MAIL))
return {"status": 200, "message": "mail sent successfully"}
except Exception as e: except Exception as e:
return "some error: {}".format(e) time.sleep(0.2)
logging.error("To: {}, error: {}".format(TO_MAIL, e))
return {"status": 500, "message": "some error: {}".format(e)}
@cherrypy.expose @cherrypy.expose
def send_mail(self): def send_mail(self):
""" """
api endpoint for send mail api endpoint for send mail
""" """
return self._send_mail() data = self._send_mail()
cherrypy.response.status = data["status"]
cherrypy.response.headers["Content-Type"] = "application/json"
return json.dumps(data["status"]).encode()
ROOT = SMTPWebhookApp() ROOT = SMTPWebhookApp()
@ -57,13 +99,18 @@ CHERRYPY_SERVER_PORT = 8081
if __name__ == "__main__": if __name__ == "__main__":
try: try:
SMTP_SERVER_HOST = os.environ["smtp_server_host"] # SMTP_SERVER_HOST = os.environ["SMTP_SERVER_HOST"]
TO_MAIL = os.environ["to_mail"] # 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"]
SMTP_SERVER_HOST = "smtp.gmail.com"
TO_MAIL = "test111@mailinator.com"
FROM_MAIL = "cis.dev393@gmail.com"
FROM_MAIL_PASSWORD = "akeel@123#"
except KeyError: except KeyError:
raise KeyError("Please check missing environment variables: to_mail, " raise KeyError("Please check missing environment variables: "
"from_mail, from_mail_password") "SMTP_SERVER_HOST, TO_MAIL, FROM_MAIL, "
"FROM_MAIL_PASSWORD")
cherrypy.server.socket_host = CHERRYPY_SERVER_HOST cherrypy.server.socket_host = CHERRYPY_SERVER_HOST
cherrypy.server.socket_port = CHERRYPY_SERVER_PORT cherrypy.server.socket_port = CHERRYPY_SERVER_PORT