Skip to content
Snippets Groups Projects
Commit b8e13184 authored by eirikmda's avatar eirikmda
Browse files

trying to send email

parent 105196e4
No related tags found
1 merge request!39Draft: Resolve "Email verification"
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)
......
# 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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment