电子邮件未在create_user上发送

时间:2019-06-03 16:11:35

标签: python flask flask-security

我正在尝试使用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()

这是我的烧瓶邮件设置

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)

3 个答案:

答案 0 :(得分:1)

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

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

这是内置的全部功能。大多数站点都将启用SECURITY_REGISTERABLE并允许用户注册-此时他们将收到电子邮件。

答案 1 :(得分:0)

在烧瓶邮件设置文件中。定义用于发送邮件的函数,如下所示:

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()

答案 2 :(得分:0)

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