From 8488446e3728d42466b6d32517c9d22c0cac71d1 Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Wed, 9 Feb 2022 22:50:16 +0530 Subject: [PATCH] Updated changes for docsrting & port updates --- README.md | 3 +-- docker-compose.yml | 4 ++-- main.py | 25 +++++++++++++------------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 477fc09..e188f42 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,7 @@ SMTP gateway accessible from InfluxDB for sending alerts. # create .env file with following parameters -server_host = 0.0.0.0 -server_port = 8081 +server_name = smtp.gmail.com to_mail = test111@mailinator.com from_mail = test@gmail.com from_mail_password = test@123 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 97c700b..4f3f7f1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ version: '3.8' services: web: build: . - ports: - - 8081:8081 + expose: + - "8081" env_file: - ./.env diff --git a/main.py b/main.py index af1688c..18cd383 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 """ -Serve cloud init files +SMTP webhook server """ import os @@ -13,20 +13,19 @@ from email.mime.text import MIMEText import cherrypy -class CloudInitApp: +class SMTPWebhookApp: """ - Serve cloud init files + SMTP webhook server """ def _send_mail(self): try: - # pylint: disable=deprecated-lambda 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('smtp.gmail.com') + client = smtplib.SMTP(SERVER_NAME) msg = MIMEText(body, 'plain', 'utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = FROM_MAIL @@ -45,26 +44,28 @@ class CloudInitApp: @cherrypy.expose def send_mail(self): """ - v1 api endpoint user-data + api endpoint for send mail """ return self._send_mail() -ROOT = CloudInitApp() +ROOT = SMTPWebhookApp() if __name__ == "__main__": try: - SERVER_HOST = os.environ["server_host"] - SERVER_PORT = int(os.environ["server_port"]) + SERVER_NAME = os.environ["server_name"] TO_MAIL = os.environ["to_mail"] FROM_MAIL = os.environ["from_mail"] FROM_MAIL_PASSWORD = os.environ["from_mail_password"] - except: # noqa:E722 + # to_mail = "test111@mailinator.com" + # from_mail = "test@gmail.com" + # from_mail_password = "test@123" + except KeyError: raise "Please check missing environment variables: to_mail, from_mail, \ from_mail_password" - cherrypy.server.socket_host = SERVER_HOST - cherrypy.server.socket_port = SERVER_PORT + cherrypy.server.socket_host = "0.0.0.0" + cherrypy.server.socket_port = 8081 ENGINE = cherrypy.engine cherrypy.tree.mount(ROOT, config={})