0

I'm trying to implement the email function inside my Flask admin panel. I've successfully integrated it within my admin panel but the problem is when I try to send any mail, it gives me below error with traceback.

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)
Jessi
  • 1,378
  • 6
  • 17
  • 37

1 Answers1

0

Ok, I found a solution.

So my importing structure was wrong

Inside def configure_mail, I have mail = Mail(app) set as global variable.

And that mail variable was not doing anything. So what I did is, inside my app.init

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

)

mail = Mail(app)

and I called this above mail into send_email.py and everything is working fine. I still can't figure out what the exact error was but this did the trick.

Jessi
  • 1,378
  • 6
  • 17
  • 37
  • 3
    don't post passwords in public questions. neither use them in your production settings. rather, do this: `MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')` – 8-Bit Borges Jan 14 '19 at 21:07