From b8e13184e352ac5e46a06bfe4e39b2b3d6e1f6ff Mon Sep 17 00:00:00 2001 From: eirikmda <eirikmda@stud.ntnu.no> Date: Tue, 23 Mar 2021 15:09:20 +0100 Subject: [PATCH] trying to send email --- api/__init__.py | 3 +- api/endpoints/emailconfirmation.py | 31 ++++++++++++++++++ api/endpoints/user.py | 51 ++++++++++++++++++++++++++++-- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/api/__init__.py b/api/__init__.py index 45fce3b..0be3b96 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -1,4 +1,5 @@ from flask import Flask +from flask_mail import Mail from flask_restful import Api from flask_bcrypt import Bcrypt from flask_jwt_extended import JWTManager @@ -6,12 +7,12 @@ from flask_sqlalchemy import SQLAlchemy from flask_limiter import Limiter from flask_limiter.util import get_remote_address - app = Flask(__name__) app.config['SECRET_KEY'] = 'randomchangethisshit' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database/database.db' app.config['JWT_AUTH_USERNAME_KEY'] = 'email' app.config['JWT_ACCESS_TOKEN_EXPIRES'] = 5 * 3600 +mail = Mail(app) db = SQLAlchemy(app) jwt = JWTManager(app) bcrypt = Bcrypt(app) diff --git a/api/endpoints/emailconfirmation.py b/api/endpoints/emailconfirmation.py index e69de29..8b001f7 100644 --- a/api/endpoints/emailconfirmation.py +++ b/api/endpoints/emailconfirmation.py @@ -0,0 +1,31 @@ +# from flask import redirect, render_template, url_for +# from api import db, limiter +# from itsdangerous import URLSafeTimedSerializer +# +# email = 'haha' +# +# ts = URLSafeTimedSerializer(app.config["SECRET_KEY"]) +# +# @app.route('/accounts/create', methods=["GET", "POST"]) +# def create_account(): +# +# # Now we'll send the email confirmation link +# subject = "Confirm your email for salamander app." +# +# token = ts.dumps(email, salt='email-confirm-key') +# +# confirm_url = url_for( +# 'confirm_email', +# token=token, +# _external=True) +# +# html = render_template( +# 'email/activate.html', +# confirm_url=confirm_url) +# +# # We'll assume that send_email has been defined in myapp/util.py +# send_email(user.email, subject, html) +# +# return redirect(url_for("index")) +# +# return render_template("accounts/create.html", form=form) \ No newline at end of file diff --git a/api/endpoints/user.py b/api/endpoints/user.py index 202ebf9..936ab15 100644 --- a/api/endpoints/user.py +++ b/api/endpoints/user.py @@ -6,8 +6,13 @@ import re from flask_jwt_extended import get_jwt_identity, jwt_required from api.forms.userforms import RegistrationForm, DeleteUserForm from validate_email import validate_email +from itsdangerous import URLSafeTimedSerializer +from api import app, mail +from flask import redirect, render_template, url_for +from flask_mail import Message EMAIL_REGEX = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$" +ts = URLSafeTimedSerializer(app.config["SECRET_KEY"]) class UserEndpoint(Resource): @@ -20,12 +25,48 @@ class UserEndpoint(Resource): if data.password.data == data.confirmPassword.data: user = db.session.query(User).filter_by(email=data.email.data.lower()).first() if not user and re.search(EMAIL_REGEX, data.email.data.lower()): - valid = validate_email(data.email.data.lower(), verify=True,check_mx=True) - if valid: + if validate_email(data.email.data.lower(), verify=True,check_mx=True): password_hash = bcrypt.generate_password_hash(data.password.data) new_user = User(name=data.name.data, email=data.email.data.lower(), pwd=password_hash) db.session.add(new_user) db.session.commit() + + # Now we'll send the email confirmation link + subject = "Confirm your email for salamander app." +# w c(..)o ( +# \__(-) __) +# /\ ( +# /(_)___) +# w /| +# | \ +# m m +# +# .="=. +# _/.-.-.\_ _ +# ( ( o o ) ) )) +# |/ " \| // +# \'---'/ // +# /`"""`\\ (( +# / /_,_\ \\ \\ +# \_\\_'__/ \ )) +# /` /`~\ |// +# / / \ / +# ,--`,--'\/\ / +# +# + token = ts.dumps(data.email.data.lower(), salt='email-confirm-key') + + # confirm_url = url_for( + # 'confirm_email', + # token=token, + # _external=True) + # + # html = render_template( + # 'email/activate.html', + # confirm_url=confirm_url) + # We'll assume that send_email has been defined in myapp/util.py + send_email(data.email.data.lower(), "hei")#, html) + return jsonify({"message": "new user created", 'status': 200}) else: return jsonify({"message": "email doesn't exist", 'status': 400}) @@ -93,3 +134,9 @@ class UserEndpoint(Resource): else: return jsonify({"message": "this user does not exist", "status": 400}) + +def send_email(email, text): + msg = Message(text, + sender="salamanderteam@noreply.com", + recipients=[email]) + mail.send(msg) \ No newline at end of file -- GitLab