Python Flask Email KeyError KeyError:'mail'

时间:2015-02-11 23:12:32

标签: python email python-2.7 flask

使用Python Flask和Flask-Mail库时遇到问题。

我收到错误:

  

KeyError:' mail'

有人可以帮我解决这个问题吗?

我的代码是:

# -*- coding: utf-8 -*-
from flask import Flask
from flask.ext.mail import Message
from flask.ext.mail import Mail
from ws import app


class Email():  

def __init__(self):
    ""
#
# Enviar el email
#
def enviar_mail(self,subject, sender, recipients, text_body="hola mundo txt", html_body="hola mundo html"):
    app = Flask(__name__)

    app.config.update(dict(
        MAIL_SERVER = 'smtp.gmail.com',
        MAIL_PORT = 465,
        MAIL_USE_TLS = False,
        MAIL_USE_SSL = True,
        MAIL_USERNAME = 'miaccount@gmail.com',
        MAIL_PASSWORD = 'mypasswd$%'
    ))

    mail = Mail(app)
    mail.init_app(app)
    msg = Message("Hello", sender="miaccount@gmail.com",recipients=["myrecipient@hotmail.com"])
    msg.body = "testing"
    msg.html = "<b>testing</b>"
    mail.send(msg)

错误

KeyError
KeyError: 'mail'
Traceback (most recent call last)
File "/home/javier/python/wszbeltia/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__

mail.send(msg)
File "/home/javier/python/wszbeltia/lib/python2.7/site-packages/flask_mail.py", line 492, in send

message.send(connection)
File "/home/javier/python/wszbeltia/lib/python2.7/site-packages/flask_mail.py", line 427, in send

connection.send(self)
File "/home/javier/python/wszbeltia/lib/python2.7/site-packages/flask_mail.py", line 190, in send

message.as_bytes() if PY3 else message.as_string(),
File "/home/javier/python/wszbeltia/lib/python2.7/site-packages/flask_mail.py", line 381, in as_string

return self._message().as_string()
File "/home/javier/python/wszbeltia/lib/python2.7/site-packages/flask_mail.py", line 307, in _message

**ascii_attachments = current_app.extensions['mail'].ascii_attachments
KeyError: 'mail'**

1 个答案:

答案 0 :(得分:5)

降级为flask_mail==0.9.0

这似乎是版本0.9.1中的回归。