消息未发送到烧杯

发布于 2025-02-13 05:12:07 字数 3374 浏览 1 评论 0原文

我在烧瓶上写了一个网站,我决定通过电子邮件创建一个密码恢复系统,使用烧结邮件为此,一切似乎都起作用,没有错误,它将其重定向到授权页面,但是邮件中没有消息

有我的configs

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME')
app.config['MAIL_DEFAULT_SENDER'] = os.getenv('MAIL_SENDER')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD')
app.config['SECRET_KEY'] = SECRET_KEY

有我的USERCLASS带有生成和验证令牌

class User(db.Model, UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(65), nullable=False, unique=True)
    email = db.Column(db.String(60), nullable=False, unique=True)
    password = db.Column(db.String(80), nullable=False)

    def get_reset_password_token(self, expires_in=1800):
        s = Serializer(app.config['SECRET_KEY'], expires_in)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_password_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except Exception:
            return
        return User.query.get(user_id)

的方法,这是我的函数发送电子邮件

def send_password_reset_email(user):
    token = user.get_reset_password_token()
    msg = Message('Запит на зміну паролю', sender='[email protected]',
                  recipients=[user.email])
    msg.body = f"""
        Для того,щоб змінити свій пароль,натисніть на наступне посилання:
        {url_for('reset_password', token=token, _external=True)}
        Якщо ви не запитували зміну паролю,протсо забийте на це повідомлення
        """
    mail.send(msg)

是处理程序密码更改页面的功能

@app.route('/reset_password_request', methods=['POST', 'GET'])
def reset_password_request():
    if current_user.is_authenticated:
        return redirect(url_for('index_page'))
    form = ResetPasswordRequestForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user:
            send_password_reset_email(user)
        return redirect(url_for('login_page'))
    return render_template('reset_password_request.html', title='Відновлення паролю', form=form,
                           css_link=css_file_reset_password_request_page)


@app.route('/reset_password/<token>', methods=['POST', 'GET'])
def reset_password(token):
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    user = User.verify_reset_password_token(token)
    if not user:
        return redirect(url_for('index_page'))
    form = ResetPasswordForm()
    if form.validate_on_submit:
        user.set_password(form.password.data)
        db.session.commit()
        flash('Пароль успішно змінений')
        return redirect(url_for('login_page'))
    return render_template('reset_password.html', form=form, title='Відновлення паролю',
                           css_file=css_file_authorization)

这两行成功执行,没有错误,但是电子邮件没有任何

flash('Пароль успішно змінений')
return redirect(url_for('login_page'))

帮助!

I write a website on Flask, I decided to create a password recovery system via email, using Flask-Mail for this, everything seems to work, there are no errors, it redirects to the authorization page, but there is no message in the mail

There is my configs

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.getenv('MAIL_USERNAME')
app.config['MAIL_DEFAULT_SENDER'] = os.getenv('MAIL_SENDER')
app.config['MAIL_PASSWORD'] = os.getenv('MAIL_PASSWORD')
app.config['SECRET_KEY'] = SECRET_KEY

There is my userclass with methods that generate and verify the token

class User(db.Model, UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(65), nullable=False, unique=True)
    email = db.Column(db.String(60), nullable=False, unique=True)
    password = db.Column(db.String(80), nullable=False)

    def get_reset_password_token(self, expires_in=1800):
        s = Serializer(app.config['SECRET_KEY'], expires_in)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_password_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except Exception:
            return
        return User.query.get(user_id)

this is my function that sends emails

def send_password_reset_email(user):
    token = user.get_reset_password_token()
    msg = Message('Запит на зміну паролю', sender='[email protected]',
                  recipients=[user.email])
    msg.body = f"""
        Для того,щоб змінити свій пароль,натисніть на наступне посилання:
        {url_for('reset_password', token=token, _external=True)}
        Якщо ви не запитували зміну паролю,протсо забийте на це повідомлення
        """
    mail.send(msg)

that is handler functions for password change pages

@app.route('/reset_password_request', methods=['POST', 'GET'])
def reset_password_request():
    if current_user.is_authenticated:
        return redirect(url_for('index_page'))
    form = ResetPasswordRequestForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user:
            send_password_reset_email(user)
        return redirect(url_for('login_page'))
    return render_template('reset_password_request.html', title='Відновлення паролю', form=form,
                           css_link=css_file_reset_password_request_page)


@app.route('/reset_password/<token>', methods=['POST', 'GET'])
def reset_password(token):
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    user = User.verify_reset_password_token(token)
    if not user:
        return redirect(url_for('index_page'))
    form = ResetPasswordForm()
    if form.validate_on_submit:
        user.set_password(form.password.data)
        db.session.commit()
        flash('Пароль успішно змінений')
        return redirect(url_for('login_page'))
    return render_template('reset_password.html', form=form, title='Відновлення паролю',
                           css_file=css_file_authorization)

These two lines are executed successfully, without errors, but nothing comes to the e-mail

flash('Пароль успішно змінений')
return redirect(url_for('login_page'))

Help me please!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文