烧瓶邮件属性错误:“功能”对象没有属性“发送”

Posted

技术标签:

【中文标题】烧瓶邮件属性错误:“功能”对象没有属性“发送”【英文标题】:flask-mail AttributeError: 'function' object has no attribute 'send' 【发布时间】:2013-12-09 18:18:52 【问题描述】:

我正在尝试使用 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】:

您也将您的视图命名为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)

【讨论】:

以上是关于烧瓶邮件属性错误:“功能”对象没有属性“发送”的主要内容,如果未能解决你的问题,请参考以下文章

烧瓶错误:“NoneType”对象没有“永久”属性

如何修复 AttributeError:“列表”对象没有属性“编码”

错误“电子邮件没有属性编码”在 python 中发送电子邮件

AttributeError:“字节”对象没有属性“告诉”

烧瓶邮件连接重置错误:[WinError 10054]

AttributeError:“字节”对象没有属性“编码”