2013-12-09 153 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

相關問題