from flask_wtf import FlaskForm
from wtforms import StringField,SubmitField,PasswordField
from wtforms.validators import DataRequired,Length,Email
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_sqlalchemy import SQLAlchemy
from flask_mail import Mail
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = 'r3t058rf3409tyh2g-rwigGWRIGh[g'
app.config['MAIL_SERVER']='smtp.googlemail.com'
app.config['MAIL_PORT']=587
app.config['MAIL_USE_TLS']=True
app.config['MAIL_USERNAME']=os.environ.get('EMAIL_USER')
app.config['MAIL_PASSWORD']=os.environ.get('EMAIL_PASS')
mail=Mail(app)
db = SQLAlchemy(app)
logMg=LoginManager(app)
logMg.login_view='login'
logMg.login_message_category='info'
bcrypt=Bcrypt()
from portfolio import routes
def send_reset_email(user):
token=user.get_reset_token()
msg=Message('Password Reset Request',sender='[email protected]',recipients=[user.email])
msg.body=''' To reset your password visit the following link:
{ url_for('reset_token',token=token,_external=True) }
If you did not Make request please contact our Team
'''
mail.send(msg)
@app.route("/reset_password",methods=['GET','POST'])
def reset_request():
if current_user.is_authenticated:
return redirect(url_for('admin'))
form=RequestResetForm()
if form.validate_on_submit():
user=User.query.filter_by(email=form.email.data).first()
send_reset_email(user)
flash('Reset Email Link Sent')
return redirect(url_for('login'))
return render_template("reset_request.html",form=form,legend='Edit Post')
@app.route("/reset_password/<token>",methods=['GET','POST'])
def reset_token():
if current_user.is_authenticated:
return redirect(url_for('admin'))
user=User.verify_reset_token(token)
if user is None:
flash('Invalid or Expired Token','warning')
return redirect(url_for(reset_request))
form=ResetPasswordForm()
if form.validate_on_submit():
hashed_password=bcrypt.generate_password_hash(form.password.data).decode('utf-8')
user.password=hashed_password
db.session.commit()
flash('Password Changed!','success')
return redirect(url_for('Login'))
return render_template('reset_token',form=form,legend='Reset Password Form')
Keep getting this error to authenticate sender I have tried changing to my email and enabling IMAP setting but did not work
smtplib.SMTPSenderRefused smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError t20sm2139075wmi.2 - gsmtp', '[email protected]')
Traceback (most recent call last) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2464, in call return self.wsgi_app(environ, start_response)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2450, in wsgi_app response = self.handle_exception(e)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask_compat.py", line 39, in reraise raise value
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask_compat.py", line 39, in reraise raise value
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functionsrule.endpoint
File "C:\Dev\Visual Studio 2019\Projects\portfolio\portfolio\routes.py", line 177, in reset_request send_reset_email(user)
File "C:\Dev\Visual Studio 2019\Projects\portfolio\portfolio\routes.py", line 168, in send_reset_email mail.send(msg)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask_mail.py", line 492, in send message.send(connection)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask_mail.py", line 427, in send connection.send(self)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\flask_mail.py", line 192, in send message.rcpt_options)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\smtplib.py", line 867, in sendmail raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError t20sm2139075wmi.2 - gsmtp', '[email protected]')
Prerequisite
Once all above prerequisite has been done, try to check that you can get those variable from command line first.
go to command prompt, type Echo %EMAIL_USER% and the expect return output is your email. if the %EMAIL_USER% also return then you configure step 2 above incorrectly.
Do not execute Python file from VS Code. This issue similar to Pycharm user as well. I think the VS Code may not be able to access OS environment somehow (possibly I do not sure how to configure that.) The alternative solution is activate your Virtual Environment via Command Line and then run Python via Command Line -- Open command prompt and go to your Python Program folder. CD Scripts and execute 'activate'
2.1 test whether your Python can get OS environment by execute Python and import os and then
print (os.environ.get("EMAIL_USER"))
The expect output is your email address.
2.2 Once it done, you go back to your main program folder and execute Python run.py
Seccond thing that you can try: instead of using TLS,
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With