From f5f74540fd2615f90432af8d34df35f53b44fdde Mon Sep 17 00:00:00 2001 From: James Wang Date: Sat, 16 Jan 2016 16:41:16 +0000 Subject: [PATCH] Implement email sending --- server/api/user.py | 6 +++--- server/api/utils.py | 14 +++++++++++++- server/app.py | 8 +++----- server/config.py | 3 +++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/server/api/user.py b/server/api/user.py index cf9b7dd..d91e8d2 100644 --- a/server/api/user.py +++ b/server/api/user.py @@ -98,7 +98,7 @@ def user_info(): me = False if not("username" in session) else username.lower() == session["username"].lower() user = get_user(username_lower=username.lower()).first() if user is None: - raise WebException("User not found.") + raise WebException("User not found.") show_email = me if logged_in else False user_in_team = in_team(user) @@ -182,7 +182,7 @@ def login_user(username, password): token = LoginTokens(user.uid, user.username, ua=useragent, ip=ip) db.session.add(token) db.session.commit() - + session["sid"] = token.sid session["username"] = token.username session["admin"] = user.admin == True @@ -215,4 +215,4 @@ def validate_captcha(form): captcha_response = form["captcha_response"] data = {"secret": "6Lc4xhMTAAAAACFaG2NyuKoMdZQtSa_1LI76BCEu", "response": captcha_response} response = requests.post("https://www.google.com/recaptcha/api/siteverify", data=data) - return response.json()["success"] \ No newline at end of file + return response.json()["success"] diff --git a/server/api/utils.py b/server/api/utils.py index 8ac6673..652382c 100644 --- a/server/api/utils.py +++ b/server/api/utils.py @@ -2,10 +2,12 @@ import datetime import json import random import re +import requests import string import traceback import unicodedata +from flask import current_app as app from functools import wraps from werkzeug.security import generate_password_hash, check_password_hash @@ -33,4 +35,14 @@ def flat_multi(multidict): for key, values in multidict.items(): value = values[0] if type(values) == list and len(values) == 1 else values flat[key] = value.encode("utf-8") - return flat \ No newline at end of file + return flat + +def send_email(recipient, subject, body): + api_key = app.config["MG_API_KEY"] + data = {"from": "EasyCTF Administrator <%s>" % (app.config["ADMIN_EMAIL"]), + "to": recipient, + "subject": subject, + "text": body + } + auth = ("api", api_key) + return requests.post("https://api.mailgun.net/v3/%s/messages" % (app.config["MG_HOST"]), auth=auth, data=data) diff --git a/server/app.py b/server/app.py index 2ec4e61..b6af1fe 100644 --- a/server/app.py +++ b/server/app.py @@ -1,6 +1,8 @@ from argparse import ArgumentParser from flask import Flask +app = Flask(__name__) + import api import config import json @@ -8,11 +10,7 @@ import os from api.decorators import api_wrapper -app = Flask(__name__) - -app.config["SQLALCHEMY_DATABASE_URI"] = config.SQLALCHEMY_DATABASE_URI -app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = config.SQLALCHEMY_TRACK_MODIFICATIONS -app.config["UPLOAD_FOLDER"] = config.UPLOAD_FOLDER +app.config.from_object(config) if not os.path.exists(app.config["UPLOAD_FOLDER"]): os.makedirs(app.config["UPLOAD_FOLDER"]) diff --git a/server/config.py b/server/config.py index eb5039e..5778f43 100644 --- a/server/config.py +++ b/server/config.py @@ -19,3 +19,6 @@ UPLOAD_FOLDER = os.path.normpath("../web/files") CTF_BEGIN = 0 # To be used later CTF_END = 0 # To be used later +MG_HOST = "" +MG_API_KEY = "" +ADMIN_EMAIL = ""