def send_activation(): username = session['auth']['username'] # creating cursor cur = mysql.connection.cursor() # getting user id & email result = cur.execute("SELECT id,email FROM users_ WHERE username = %s", [username]) if result > 0: data = cur.fetchone() email = data['email'] id = data['id'] activation_link = create_activation_link(str(id)) msg = Message("Your Flasky-App account activation link", sender=app.config['MAIL_DEFAULT_SENDER'], recipients=[email]) msg.html = activation_mail_body(username, request.url_root, activation_link) try: mail.send(msg) except: app.logger.info('Failed to send mail; check your SMTP connexion') flash("The activation link was sent successfully to %s!" % email, "success") else: flash("Failed to create the activation link", "danger") return redirect(url_for("dashboard"))
def iforgot(): form = ForgotPwForm(request.form) if request.method == 'POST' and form.validate(): email = request.form['email'] cur = mysql.connection.cursor() result = cur.execute( "SELECT id, username FROM users_ WHERE email = %s", [email]) if result > 0: usr = cur.fetchone() reset_link = create_pwreset_link(str(usr['id'])) msg = Message("Flasky-App password reset", sender=app.config['MAIL_DEFAULT_SENDER'], recipients=[email]) msg.html = pwreset_mail_body(usr['username'], request.url_root, reset_link) try: mail.send(msg) except: app.logger.info( 'Failed to send mail; check your SMTP connexion') cur.close() return redirect(url_for('iforgot', sent='true')) is_sent = request.args.get('sent') if is_sent == 'true': to_return = render_template('auth/forgot-password.html', sent=1) else: to_return = render_template('auth/forgot-password.html', sent=0, form=form) return to_return
def send_reset_email(user): token = user.get_reset_token() msg = Message("Flasky Bolg,Reset password", sender="*****@*****.**", recipients=["*****@*****.**"]) msg.body = f" the link for reset" \ f"{url_for('reset_request',token = token)}" \ f" ignore" mail.send(msg)
def register(): form = RegisterForm(request.form) if request.method == 'POST' and form.validate(): # getting form values name = form.name.data email = form.email.data username = form.username.data password = sha256_crypt.encrypt(str(form.password.data)) # creating cursor cur = mysql.connection.cursor() # checking if user email or username already exists result = cur.execute( "SELECT * FROM users_ WHERE email=%s OR username=%s", [email, username]) if result > 0: flash("This email address or username is already taken.", "danger") return redirect(url_for("register")) else: # submit to DB cur.execute( "INSERT INTO users_ (name,email,username,password) VALUES (%s, %s, %s, %s)", (name, email, username, password)) # commit changes mysql.connection.commit() # getting user id & creating activation link cur.execute("SELECT id FROM users_ WHERE username = %s", [username]) user = cur.fetchone() activation_link = create_activation_link(str(user['id'])) # sending mail with activation link msg = Message("Your Flasky-App account activation link", sender=app.config['MAIL_DEFAULT_SENDER'], recipients=[email]) msg.html = activation_mail_body(username, request.url_root, activation_link) try: mail.send(msg) except: app.logger.info( 'Failed to send mail; check your SMTP connexion') # close connection cur.close() flash("You are now registered and can log in!", "success") return redirect(url_for("login")) else: return render_template('auth/register.html', form=form)
def send_async_email(app, msg): with app.app_context(): mail.send(msg)