gpt4 book ai didi

python - flask 邮件 AttributeError : 'function' object has no attribute 'send'

转载 作者:行者123 更新时间:2023-11-28 21:57:02 30 4
gpt4 key购买 nike

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

最佳答案

您还命名了您的 View 邮件:

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

这是在您在 View 中引用 mail 时发现的,而不是 Mail() 实例。重命名 View 或重命名对 Mail() 对象的引用。

将您的 View 重命名为 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)

关于python - flask 邮件 AttributeError : 'function' object has no attribute 'send' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20477749/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com