flask-mail AttributeError:'function'对象没有属性'send'

时间:2013-12-09 18:18:52

标签: python flask flask-mail

我正在尝试使用flask.ext.mail发送电子邮件,但是我收到以下错误。我遵循了一些教程,他们似乎都在做同样的事情,我一直在四处寻找是否有人收到此错误并且没有找到它。:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site xpackages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/aaronwishnick/Documents/Work/NewPersonalSite/app/views.py", line 27, in mail
    mail.send(msg)
AttributeError: 'function' object has no attribute 'send'

这是我的 init .py

import os
from flask import Flask
from flask.ext.mail import Mail
app = Flask(__name__)
app.config.update(
    MAIL_SERVER = 'smtp.gmail.com',
    MAIL_PORT = 25,
    MAIL_USE_TLS = False,
    MAIL_USE_SSL = False,
    MAIL_USERNAME = 'gmail_username',
    MAIL_PASSWORD = 'gmail_password'
)
mail = Mail(app)
from app import views

邮件功能:

email = request.args.get('email')
name = request.args.get('name')
message = request.args.get('message')
msg = Message("Message from your site",
              sender=email,
              recipients=["aaronwishnick@gmail.com"])
msg.body = message
mail.send(msg)

1 个答案:

答案 0 :(得分:5)

您也将视图 mail命名为:

  File "....", line 27, in mail

当您在视图中引用mail而非Mail()实例时,会发现此问题。重命名视图或将引用重命名为Mail()对象。

将您的视图重命名为send_mail,例如:

def send_mail():
    email = request.args.get('email')
    name = request.args.get('name')
    message = request.args.get('message')
    msg = Message("Message from your site",
                  sender=email,
                  recipients=["aaronwishnick@gmail.com"])
    msg.body = message
    mail.send(msg)