4

这里是我用来发送邮件以重置密码的主要烧瓶邮件代码,以防用户忘记但当我运行和编码时它看到我 smtplib.SMTPServerDisconnected: Connection unexpectedly closed

下面是__init__.py文件代码

import os
from flask_mail import Mail
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager

app = Flask(__name__)

app.config['SECRET_KEY'] = '7f09c01a4f942b0812be4cb86e065f77'
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///site.db'

db=SQLAlchemy(app)
bcrypt=Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view='login'
login_manager.login_message_category='info'

app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = 'sender@gmail.com'
app.config['MAIL_PASSWORD'] = 'password'

mail = Mail(app)

from index import routes

routes.py文件

import os
import binascii
from PIL import Image
from flask import render_template,url_for, flash, redirect, request, abort
from index import app,db,bcrypt,mail
from index.forms import (RegistrationForm,LoginForm,UpdateAccountForm,PostForm,
                        RequestResetForm,ResetPasswordForm)
from index.models import User, Post
from flask_login import login_user, current_user, logout_user, login_required
from flask_mail import Message

token = user.get_reset_token()
    msg = Message('Password Reset Request',
                  sender="ketansuthar899@gmail.com",
                  recipients=[user.email])
    msg.body = '''To reset your password, visit the following link:{}
If you did not make this request then simply ignore this email and no changes will be made.
'''.format((url_for('reset_token', token=token, _external=True)))

    mail.send(msg)

这是 get_reset_token 方法:

from datetime import datetime
from index import db, login_manager,app
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask_login import UserMixin

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

我还在 StackOverflow 上看到了下面的代码,但这也没有用

app.config['MAIL_USE_SSL'] = True
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'sender@gmail.com'
app.config['MAIL_PASSWORD'] = 'password'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
4

0 回答 0