【问题标题】:Email not sending on create_user电子邮件未在 create_user 上发送
【发布时间】:2019-10-19 05:23:35
【问题描述】:

我正在尝试使用烧瓶安全性,并且在调用 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)

【问题讨论】:

  • 你需要使用flask-mail扩展名。
  • The Sparks Foundation 的技术人员,我添加了更多代码,用于通过烧瓶邮件发送电子邮件。我相信您在没有调用烧瓶邮件的问题上是正确的。只是不知道如何在这个实例中使用它
  • 您需要明确发送消息。通过创建消息对象。
  • Sparks 基金会的技术人员,请您给我一个基本的例子。这让我一直在兜圈子,以至于现在我无法清楚地思考
  • 您需要在配置对象中添加SECURITY_EMAIL_SENDER= youremail@mail.com

标签: python flask flask-security


【解决方案1】:

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

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

【讨论】:

  • 好的,我相信这会起作用,但我希望更多地使用flask-security附带的内置方法,以避免以后出现重置密码等功能的问题
  • 参考thisthis
  • 我都经历过很多次。我仍然不明白如何让烧瓶安全的预期功能发挥作用。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 2014-10-26
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      相关资源
      最近更新 更多