简体   繁体   English

flask 的 Flask-Mail 扩展在应用程序工厂结构中不起作用

[英]Flask-Mail extension of flask not working in application factory structure

What I am trying to do is to configure my flask-mail extension in application factory in order to send mail from different.py files but I am unable to do so.I know my configuration are right because I had same config settings when using single file structure __init__.py我想要做的是在应用程序工厂中配置我的烧瓶邮件扩展,以便从不同的.py 文件发送邮件,但我无法这样做。我知道我的配置是正确的,因为我在使用单一时具有相同的配置设置文件结构__init__.py

from flask import Flask
from flask_mail import Mail,Message
def create_app():
    mail = Mail()
    app.config['MAIL_SERVER']='smtp.gmail.com'
    app.config['MAIL_PORT']=465
    app.config['MAIL_USE_SSL']=True
    app.config['MAIL_USERNAME'] = ''
    app.config['MAIL_PASSWORD'] = ''
    app.config['MAIL_DEFAULT_SENDER'] = ''
    app.config['MAIL_USE_TLS'] = False 
    mail.init_app(app)
    return app

and the file I am willing to send emails from: dev.py以及我愿意发送电子邮件的文件: dev.py

from flask import *
@bp.route('/login',methods=['GET','POST'])
def login():
    if request.method == 'POST':
        emailid = request.form['emailid']
        msg = Message('Password Change', sender = '')
        msg.add_recipient(emailid)
        msg.html = "<b>MADHAV</b>"
        mail.send(msg)

The error I am receiving is following:我收到的错误如下:

NameError: name 'Message' is not defined

By looking at the imports you forgot to add the Message object in there.通过查看您忘记在其中添加Message object 的导入。

If that s the only error you get, the following line should solve the issue.如果这是您得到的唯一错误,那么以下行应该可以解决问题。

dev.py开发者.py

from flask-mail import Message

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM