flask-mail AttributeError: 'function' object has no attribute 'send'

5.6k views Asked by At

I am trying to use flask.ext.mail to send email, however I am getting the following error. I followed a number of tutorials and they all seem to be doing the same thing, I have been looking around to see if anyone is getting this error and haven't found it.:

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'

Here is my 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

And mail function:

email = request.args.get('email')
name = request.args.get('name')
message = request.args.get('message')
msg = Message("Message from your site",
              sender=email,
              recipients=["[email protected]"])
msg.body = message
mail.send(msg)
1

There are 1 answers

0
Martijn Pieters On BEST ANSWER

You named your view mail as well:

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

This is found when you refer to mail in your view, not the Mail() instance. Rename the view or rename the reference to the Mail() object.

Rename your view to send_mail for example:

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=["[email protected]"])
    msg.body = message
    mail.send(msg)