def forgot(): ''' Functions to handle cases when user forgets email id Redirect user to index page after ''' form = user_forms.Forgot() if form.validate_on_submit(): user = models.User.query.filter_by(email=form.email.data).first() # Check the user exists if user is not None: # Subject of the confirmation email subject = 'Reset your password.' # Generate a random token token = ts.dumps(user.email, salt='password-reset-key') # Build a reset link with token resetUrl = url_for('userbp.reset', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/reset.html', reset_url=resetUrl) # Send the email to user email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to reset your password.', 'positive') return redirect(url_for('index')) else: flash('Unknown email address.', 'negative') return redirect(url_for('userbp.forgot')) return render_template('user/forgot.html', form=form)
def forgot(): form = user_forms.Forgot() if form.validate_on_submit(): user = models.User.query.filter_by(email=form.email.data).first() # Check the user exists if user is not None: # Subject of the confirmation email subject = 'Reset your password.' # Generate a random token token = ts.dumps(user.email, salt='password-reset-key') # Build a reset link with token resetUrl = url_for('userbp.reset', token=token, _external=True) # Render an HTML template to send by email html = render_template('email/reset.html', reset_url=resetUrl) # Send the email to user message = Mail(from_email='*****@*****.**', to_emails=user.email, subject=subject, html_content=render_template('email/reset.html', reset_url=resetUrl)) try: sg = SendGridAPIClient( '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' ) response = sg.send(message) print(response.status_code) print(response.body) print(response.headers) except Exception as e: print(e.message) #email.send(user.email, subject, html) # Send back to the home page flash('Check your emails to reset your password.', 'positive') return redirect(url_for('index')) else: flash('Unknown email address.', 'negative') return redirect(url_for('userbp.forgot')) return render_template('user/forgot.html', form=form)