Flask-Security电子邮件确认(Flask-Mail)无法修改flask_security / utils.py而无法发送

时间:2017-07-20 17:50:49

标签: python flask flask-security flask-mail

我正在尝试使用Flask-Security,但在收到要发送的确认电子邮件时遇到了一些问题。我最终通过删除flask_security / utils.py中的一行来修复它。我删除了第387行,强制使用邮箱邮件来使用app.config的邮件发件人。

386: msg = Message(subject,
387:               sender=_security.email_sender,
388:               recipients=[recipient])

在删除之前,代码将在flask_mail.py,@ line 105(在sanatize_address方法内)失败,因为传入的addr只是一个字符串,而不是元组。

102: def sanitize_address(addr, encoding='utf-8'):
103:     if isinstance(addr, string_types):
104:         addr = parseaddr(force_text(addr))
105:     nm, addr = addr

我希望能够运行我的代码而无需在每次安装时修改flask_security / utils.py。有什么建议?这可能是我错过的配置中的一个步骤,但我无法告知烧瓶安全的文档(他们有点受限)。

感谢您的帮助,下面是我的示例应用。

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore, \
    UserMixin, RoleMixin, login_required
import flask_security.utils
import flask_mail

# Create app
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'

# Flask-Security Config
app.config['SECURITY_PASSWORD_SALT'] = 'super_secret'
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_TRACKABLE'] = True
app.config['SECURITY_CONFIRMABLE'] = True
app.config['SECURITY_RECOVERABLE'] = True

# Mail Config
app.config['MAIL_SERVER'] = 'smtp.googlemail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'example@gmail.com'
app.config['MAIL_PASSWORD'] = 'password'
app.config['MAIL_DEFAULT_SENDER'] = app.config['MAIL_USERNAME']

mail = flask_mail.Mail(app)

# Create database connection object
db = SQLAlchemy(app)

# Define models
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    username = db.Column(db.String(255))
    password = db.Column(db.String(255))
    last_login_at = db.Column(db.DateTime())
    current_login_at = db.Column(db.DateTime())
    last_login_ip = db.Column(db.String(100))
    current_login_ip = db.Column(db.String(100))
    login_count = db.Column(db.Integer)
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary='roles_users',
                        backref=db.backref('users', lazy='dynamic'))

# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

# Create a user to test with
@app.before_first_request
def create_user():
    db.create_all()
    user_datastore.create_user(email='user@website.com', password=flask_security.utils.hash_password('password'))
    db.session.commit()

# Views
@app.route('/')
@login_required
def home():
    return('Here you go!')

if __name__ == '__main__':
    app.run()

1 个答案:

答案 0 :(得分:3)

我最近也发现了这一点。检查源代码后,我找到原因。 ps:我是一个新的python学习者,所以也许,以下是错误的。

在您的代码中app.config['MAIL_DEFAULT_SENDER'] = app.config['MAIL_USERNAME']。因此,让我们检查源代码,找出flask-security如何使用此配置。

在flask-security / core.py中:

41: _default_config = {
...
87:     'EMAIL_SENDER': LocalProxy(lambda: current_app.config.get(
88:         'MAIL_DEFAULT_SENDER', 'no-reply@localhost.com'
89:     )),

如果未设置SECURITY_EMAIL_SENDER配置,则flask-security使用lambda: current_app.config.get('MAIL_DEFAULT_SENDER', 'no-reply@localhost.com')的代理设置SECURITY_EMAIL_SENDER。注意:SECURITY_EMAIL_SENDER目前是LocalProxy个实例。

386: msg = Message(subject,
387:               sender=_security.email_sender,
388:               recipients=[recipient])

_security.email_senderLocalProxy个实例。

102: def sanitize_address(addr, encoding='utf-8'):
103:     if isinstance(addr, string_types):
104:         addr = parseaddr(force_text(addr))
105:     nm, addr = addr

addrLocalProxy个实例。因此isinstance(addr, string_types)Falsenm, addr = addr出错。

因此,设置SECURITY_EMAIL_SENDER配置以避免在使用flask-security时出现此错误。