Added Send mail functionality & Dockerized application #1
|
@ -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
|
||||
PeterSurda marked this conversation as resolved
Outdated
|
||||
to_mail = test111@mailinator.com
|
||||
from_mail = test@gmail.com
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
add smtp server name add smtp server name
|
||||
from_mail_password = test@123
|
|
@ -3,7 +3,7 @@ version: '3.8'
|
|||
services:
|
||||
web:
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`smtp-gateway:`
|
||||
build: .
|
||||
ports:
|
||||
- 8081:8081
|
||||
expose:
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
perhaps this should be inside the perhaps this should be inside the `Dockerfile`
|
||||
- "8081"
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
If we only use this internally inside a docker service, we should use If we only use this internally inside a docker service, we should use `expose`, not `port`.
|
||||
env_file:
|
||||
- ./.env
|
||||
|
|
25
main.py
25
main.py
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
Serve cloud init files
|
||||
SMTP webhook server
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
change docstring change docstring
|
||||
"""
|
||||
|
||||
import os
|
||||
|
@ -13,20 +13,19 @@ from email.mime.text import MIMEText
|
|||
import cherrypy
|
||||
|
||||
|
||||
class CloudInitApp:
|
||||
class SMTPWebhookApp:
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
class name class name
|
||||
"""
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
should log to stdout or stderr (research which one is more appropriate) should log to stdout or stderr (research which one is more appropriate)
|
||||
Serve cloud init files
|
||||
SMTP webhook server
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
docstring docstring
|
||||
"""
|
||||
|
||||
PeterSurda marked this conversation as resolved
PeterSurda
commented
this should go under this should go under `__main__`
|
||||
def _send_mail(self):
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
remove remove `CONFIG` and only use environment variables
if some variable missing, display a helpful error message and quit with non-zero error code
|
||||
try:
|
||||
# pylint: disable=deprecated-lambda
|
||||
cl = cherrypy.request.headers['Content-Length']
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
override from env variables override from env variables
|
||||
rawbody = cherrypy.request.body.read(int(cl))
|
||||
req_body = json.loads(rawbody)
|
||||
subject = req_body['subject']
|
||||
body = req_body['body']
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
debug we don't need debug we don't need
|
||||
client = smtplib.SMTP('smtp.gmail.com')
|
||||
client = smtplib.SMTP(SERVER_NAME)
|
||||
msg = MIMEText(body, 'plain', 'utf-8')
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
port 587 port 587
|
||||
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
|
||||
"""
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
docstring docstring
|
||||
return self._send_mail()
|
||||
|
||||
|
||||
ROOT = CloudInitApp()
|
||||
ROOT = SMTPWebhookApp()
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`except (SMTPConnectionError, TimeoutError) as e:`
|
||||
|
||||
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"]
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`SERVER_PORT` should default to `587` and be an integer.
|
||||
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"
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`except KeyError`
|
||||
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"
|
||||
PeterSurda marked this conversation as resolved
Outdated
PeterSurda
commented
`except smtplib.SMTPException`
|
||||
cherrypy.server.socket_port = 8081
|
||||
ENGINE = cherrypy.engine
|
||||
|
||||
cherrypy.tree.mount(ROOT, config={})
|
||||
|
|
Loading…
Reference in New Issue
Block a user
remove as it's matched with docker service