电子邮件未发送 create_user

Email not sending on create_user

我正在尝试使用 flask-security 并且在调用 create_user 时无助地尝试发送电子邮件。 正在创建用户。这些角色正在发挥作用,但是,我找不到任何说明如何在用户注册时向其发送电子邮件的文档。

为简单起见,我只是使用文档中的代码在首次请求之前创建用户。

# Create a user to test with

@app.before_first_request
def create_user():
    db.create_all()

# Create the Roles "admin" and "office_owner" -- unless they already exist
    user_datastore.find_or_create_role(name='admin', description='Administrator')
    user_datastore.find_or_create_role(name='office_owner', description='Office owner')

    if not user_datastore.get_user('test@gmail.com'):
        user_datastore.create_user(email='test@gmail.com', password=flask_security.utils.hash_password('password'))

    # Commit any database changes; the User and Roles must exist before we can add a Role to the User
        db.session.commit()



    db.session.commit()

这是我的 flask-mail 设置

from flask import Flask
from flask_mail import Mail, Message
import os

mail_keys = {
  'password_key': os.environ['EMAIL_PASSWORD'],
  }

app =Flask(__name__)
mail=Mail(app)



app.config['MAIL_SERVER']='smtp.sendgrid.net'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'apikey'
app.config['MAIL_PASSWORD'] = mail_keys['password_key']
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail=Mail(app)

config.py 是

import os
basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):


SQLALCHEMY_DATABASE_URI='postgresql://postgres///'

SQLALCHEMY_TRACK_MODIFICATIONS = False

SECURITY_PASSWORD_SALT = 'hjdsafjkhalkj'
SECURITY_PASSWORD_HASH='bcrypt'
SECURITY_CONFIRMABLE=True
SECURITY_REGISTERABLE=True
SECURITY_RECOVERABLE=True
SECURITY_CHANGEABLE=True

settings.py

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config import Config
from flask_mail import Mail, Message
from flask_migrate import Migrate

app=Flask(__name__)
app.config.from_object(Config)

mail=Mail(app)
db=SQLAlchemy(app)
migrate = Migrate(app, db)

在您的 flask-mail 设置文件中。像这样定义一个发送邮件的函数:

def send_email(subject, sender, recipients, text_body, html_body):
   msg = Message(subject, sender=sender, recipients=recipients)
   msg.body = text_body
   msg.html = html_body
   mail.send(msg)

在任何您想触发邮件操作的地方调用此方法。例如:

@app.before_first_request
def create_user(): 
   db.create_all() # Create the Roles "admin" and "office_owner" -- unless they already exist
   user_datastore.find_or_create_role(name='admin', description='Administrator')
   user_datastore.find_or_create_role(name='office_owner', description='Office owner')
   if not user_datastore.get_user('test@gmail.com'):
     user_datastore.create_user(email='test@gmail.com', password=flask_security.utils.hash_password('password')) # Commit any database changes; the User and Roles must exist before we can add a Role to the User    
     db.session.commit()
     send_mail(subject="AccountCreation",Sender="somesender",recepient="somerecepient",text_body="congratulations account created ",text_html=None)
   db.session.commit()

最简单的答案是 create_user 是一个管理功能。 Flask-Security 对此一无所知,因为它与视图无关。

开箱即用 - 如 configuration.rst 中所述,它可以发送以下邮件: SEND_REGISTER_EMAILSEND_PASSWORD_CHANGE_EMAILSEND_PASSWORD_RESET_EMAILSEND_PASSWORD_RESET_NOTICE_EMAIL

这就是内置的全部内容。大多数网站会启用 SECURITY_REGISTERABLE 并允许用户注册 - 此时他们会收到一封电子邮件。

已经有一段时间了,但是 在 app.config 中 TESTING = True 时您无法发送电子邮件 :)