请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

ImportError: cannot import name 'mail'一直在报错,请问老师该怎么解决啊


from app import mail
from threading import Thread
from flask_mail import Message
from flask import current_app, render_template



def send_async_email(app, msg):
    with app.app_context():
        try:
            mail.send(msg)
        except Exception as e:
            pass


def send_mail(to, subject, template, **kwargs):
    # Python email
    # msg = Message('测试邮件', sender='aaa@qq.com', body='Test',
    #               recipients=['user@qq.com'])
    msg = Message('[鱼书]' + ' ' + subject,
                  sender=current_app.config['MAIL_USERNAME'],
                  recipients=[to])
    msg.html = render_template(template, **kwargs)
    # current_app  app = Flask()
    app = current_app._get_current_object()
    thr = Thread(target=send_async_email, args=[app, msg])
    thr.start()

from flask import Flask
from flask_login import LoginManager

from app.models.book import db
from flask_mail import Mail

login_manager = LoginManager()
mail = Mail()

def create_app():
app = Flask(name)
app.config.from_object(‘app.secure’)
app.config.from_object(‘app.setting’)
register_blueprint(app)

db.init_app(app)
login_manager.init_app(app)
login_manager.login_view = 'web.login'
login_manager.login_message = '请先登录或注册'

mail.init_app(app)

with app.app_context():
    db.create_all()
return app

def register_blueprint(app):
from app.web.book import web
app.register_blueprint(web)

正在回答 回答被采纳积分+3

1回答

7七月 2019-04-04 03:03:09

具体是报什么错误呢?只能看到错误 才能大概猜到是什么原因。

0 回复 有任何疑惑可以回复我~
  • 提问者 怕几眼 #1
    File "C:\Users\Lenovo\Desktop\fisher\app\libs\email.py", line 1, in <module>
        from app import mail
    ImportError: cannot import name 'mail'
    
    Process finished with exit code 1
    回复 有任何疑惑可以回复我~ 2019-04-04 09:41:27
  • 提问者 怕几眼 #2
    请教老师
    回复 有任何疑惑可以回复我~ 2019-04-04 09:42:14
  • 7七月 回复 提问者 怕几眼 #3
    是不是引入的路径不对?可以对一下源码看看
    回复 有任何疑惑可以回复我~ 2019-04-05 03:55:46
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信