2016-12-12 10 views
0

Я пытаюсь реализовать функцию электронной почты внутри моей панели управления Flask. Я успешно интегрировал его в свою панель администратора, но проблема в том, когда я пытаюсь отправить любую почту, это дает мне ниже ошибку с трассировкой.Flask Security, Flask Mail AttributeError: объект «NoneType» не имеет атрибута «send»

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' 

send_email.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) 

flask_security.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) 

ответ

0

Хорошо, я нашел решение.

Так моя структура импорта была неправильно

Внутри четкости configure_mail, у меня есть почта = Mail (приложение) установить в качестве глобальной переменной.

И эта переменная не делала ничего. Так что я сделал, внутри своего приложения. INIT

app.config.update(
MAIL_SERVER='smtp.gmail.com', 
MAIL_PORT=587, 
MAIL_USE_SSL=False, 
MAIL_USERNAME='gopokes5326', 
MAIL_PASSWORD='rkswldi86', 
MAIL_USE_TLS=True, 
DEFAULT_MAIL_SENDER='Danny from DPC' 
# SECRET_KEY='abcdefd_thats_a_charming_secret_key', 

)

mail = Mail(app) 

и я назвал эту почту выше в send_email.py и все работает нормально. Я до сих пор не могу понять, что такое точная ошибка, но это сделало трюк.

 Смежные вопросы

  • Нет связанных вопросов^_^