2013-12-09 305 views
2

我想使用flask.ext.mail发送电子邮件,但是我收到以下错误消息。我跟着一些教程,他们似乎都做同样的事情,我一直在四处寻找,看是否有人得到这个错误,并没有发现它。:flask-mail AttributeError:'function'object has no attribute'send'

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=["[email protected]"]) 
msg.body = message 
mail.send(msg) 

回答

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=["[email protected]"]) 
    msg.body = message 
    mail.send(msg) 
+0

哇,太感谢你了,从来没有甚至想到! – awishn02

相关问题