我正在尝试在我的 Flask 管理面板中实现电子邮件功能。我已经成功地将它集成到我的管理面板中,但问题是当我尝试发送任何邮件时,它给了我下面的回溯错误。
builtins.AttributeError
AttributeError: 'NoneType' object has no attribute 'send'
File
"/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_admin/base.py", line 69, in inner
return self._run_view(f, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/flask_admin/base.py", line 368, in _run_view
return fn(self, *args, **kwargs)
File "/Users/genomics/PycharmProjects/side_project_one/app/security_layer.py", line 44, in index
send_email(admin_form.subject.data, admin_form.sender.data, [i], admin_form.content.data, None)
File "/Users/genomics/PycharmProjects/side_project_one/app/views/send_email.py", line 41, in send_email
mail.send(msg)
AttributeError: 'NoneType' object has no attribute 'send'
发送电子邮件.py
mail = None
def configure_mail(app):
# EMAIL SETTINGS
global mail
app.config.update(
MAIL_SERVER='smtp.gmail.com',
MAIL_PORT=587,
MAIL_USE_SSL=False,
MAIL_USERNAME='xxxx',
MAIL_PASSWORD='xxxx',
MAIL_USE_TLS=True,
DEFAULT_MAIL_SENDER='Danny from DPC'
#SECRET_KEY='abcdefd_thats_a_charming_secret_key',
)
mail = Mail(app)
def send_email(subject, sender, recipients, text_body, html_body):
msg = Message(subject, sender=sender, recipients=recipients)
msg.body = text_body
msg.html = html_body
mail.send(msg)
烧瓶安全.py
from app.views.login_forms import AdminForm
from app.views.send_email import send_email
class EmailRegisterView(BaseView):
def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
if current_user.has_role('admin_danny'):
return True
return False
@expose('/', methods=['GET', 'POST'])
def index(self):
admin_form = AdminForm()
if request.method == 'POST':
if admin_form.validate_on_submit():
recipients = [admin_form.recipient.data]
divided_recipients = recipients[0].split(',')
for i in divided_recipients:
send_email(admin_form.subject.data, admin_form.sender.data, [i], admin_form.content.data, None)
return self.render('admin.html', form=admin_form)