Search code examples
pythonemailpython-2.7flask

Python Flask Email KeyError KeyError: 'mail'


I have a problem using Python Flask and Flask-Mail libraries.

I'm getting a error:

KeyError: 'mail'

Can someone help me resolve this?

My code is:

# -*- 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 = '[email protected]',
        MAIL_PASSWORD = 'mypasswd$%'
    ))

    mail = Mail(app)
    mail.init_app(app)
    msg = Message("Hello", sender="[email protected]",recipients=["[email protected]"])
    msg.body = "testing"
    msg.html = "<b>testing</b>"
    mail.send(msg)

And the error is

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'**

Solution

  • Downgrade to flask_mail==0.9.0

    This appears to be a regression in version 0.9.1.