Forums

Having trouble with Importing Flask-Mail

Hello,

I have an app that I'm trying to integrate Flask-Mail. I get the following errors when I restart my app.

2012-10-12 18:40:41,237 :Traceback (most recent call last):
2012-10-12 18:40:41,237 :ImportError: No module named mail

I used a bash console and used easy_install to install Flask-Mail and it now resides in:

/ > home > MorePyPlease > .local > lib > python2.7 > site-packages

A recent callback in my application was here (unrelated to this error):

File "/usr/local/lib/python2.7/site-packages/Flask-0.9-py2.7.egg/flask/app.py", line 1344, in dispatch_request

which leads me to believe I am using Python2.7.. But I believe I started coding my appliation before you switched over. Just for the information :)

What am I doing wrong?

In my app I call it like this:

from flaskext.mail import Mail, Message
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('MINITWIT_SETTINGS', silent=True)

# setup Mail
mail = Mail(app)

Is there additional steps I need to take before it will recognize it as a flaskext? should I be importing it another way?

Okay. I've actually resolved this. Apparantly the readthedocs page for flask-mail is older than Flask-Mail and there was a module change.

You now import flask_mail not flaskext.mail.

Despite this, is there any reason that I can't use gmail as a mail handler? I noticed it's not on the "approved sites" list and emails aren't coming through but I'm getting no errors in my error log either.

Well, I got it this far: 2012-10-12 20:20:03,007 :smtplib.SMTPAuthenticationError: (535, '5.7.1 Please log in with your web browser and then try again. Learn more at\n5.7.1 https://support.google.com/mail/bin/answer.py?answer=78754 gx8sm8088776qab.12')

But now I am getting a 504 Gateway Timeout on my site.. Ergh.

Has anyone else had any success with flask-mail? I can't get it to work for the life of me running on here.

Here's my settings

from flask_mail import Mail, Message
app = Flask(__name__)
mail = Mail(app)
app.config.from_object(__name__)
app.config.from_envvar('MINITWIT_SETTINGS', silent=True)
app.config.update(
    DEBUG = True,
    # Flask-Mail Configuration
    MAIL_SERVER = smtp.gmail.com',
    MAIL_PORT = 587,
    MAIL_USE_TLS = True,
    MAIL_USE_SSL = False,
    MAIL_USERNAME = 'gmailuser@gmail.com',
    MAIL_PASSWORD = 'gmailpass',
    DEFAULT_MAIL_SENDER = 'gmailuser@gmail.com'
    )

# setup Mail
mail = Mail(app)


:::python
@app.route('/testmail')
def mail_it():
    """handles our message notification"""
    msg = Message("Hello",
                  sender=("Gmail User", "gmailuser@gmail.com"),recipients=["recipient@gmail.com"])
    msg.body = "Test!"
    mail.send(msg)
    return "I sent an email!"

I get no errors (got that one auth one once and went in to gmail and cleared it) and otherwise I always get my "I sent an email!" returned.

This is what i've been going off of (I've tried both combinatoins of TLS, SSL and ports). http://flask.pocoo.org/snippets/85/

Have you seen this?

Funny, looks like you two cross-posted.

@Glenn, yes. I referenced that in my last comment.

Definitely no firewall issues connecting to port 587 of smtp.gmail.com:

>>> import socket
>>> sock = socket.socket()
>>> sock.connect(("smtp.gmail.com", 587))
>>> sock.send("EHLO\n")
5
>>> sock.recv(1024)
'220 mx.google.com ESMTP a8sm7318965qew.11\r\n250-mx.google.com at your service [...]\r\n'

Do you get anything useful if you enable app.debug?

debug is enabled above and doesn't seem to affect anything when running my app. That may be my own shortcoming though. The only area i've ever seen any logging or debugging info is in my error.log when I get an unhandled exception or otherwise. I never get any other debug info and I'm not sure why (whether or not app.debug is set to True). I actually have it set twice in my app right now, once as an Environment variable and again in the app.config.update when I setup my mail settings. I'm using the Flask "Minitwit" app from github as a base for my program so I would assume it is setup correctly syntactically.

I'm really having trouble here with this. I scrubbed my app and instead copy and pasted in directly the Snippet from the linked website and tried with several google credentials with no success. I then signed up for a free account at SMTP2GO.com and have tried running the same app snippet using smtp credentials through them on every port offered. I get no errors and no mail sent!

Something is very strange..

Alright, I think this is a manner of firewall afterall. I put the gmail information back in and found out that there is a default flag for failing silently that is set to TRUE instead of FALSE. Once I disabled the silent failing it caught my error.

2012-10-13 03:53:56,250 : File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection 2012-10-13 03:53:56,258 : raise err 2012-10-13 03:53:56,258 :socket.error: [Errno 101] Network is unreachable

Why is smtp.gmail.com unreachable?

It just occurred to me that my test above probably wasn't as useful as it seemed. I ran that code from a standard console, but it occurs to me that I suspect PA runs web applications under a slightly different environment (one clue being the issue raised in this post).

So, it's entirely possible that the web applications are firewalled differently, or perhaps behind some sort of of NAT which is interfering. Still, I'm quite surprised at the network is unreachable error - that typically means the host can't find a route to the destination so isn't even attempting to open a connection, and that would typically not be due to a firewall but simply a lack of a default gateway or similar issue.

I think we'd need one of the PA staff to shed some light about what in the web app environment could be causing this.

@MorePyPlease, I've just noticed that you're on a free account. We only allow HTTP connections out and then only to a small set of white-listed sites. SMTP will not work on a free account.

Oh! Well bummer. Guess you'll be receiving a paying customer soon :)

@glenn: That makes sense. As an aside, any idea why I could connect to the SMTP server in my own Python instance? Does the "HTTP only" restriction only apply to web applications and not interactive sessions?

@Cartroo: Nice find. I duplicated your results on a free account.

Looking forward to all the PRO Goodies. You've got another $9/mo sub :) Thanks again for sorting it out!

Welcome to the land of the Yellow stars!!

Guess I am going to have to sign up too thanks for this thread guys

Hi!

I solved this via https://accounts.google.com/b/0/DisplayUnlockCaptcha on the relevant gmail account.

please note that you can change the python version from the web tab under the "Code" title . There is no need to delete the app . And make sure to unable a security feature in Gmail service that may block sending your Email . here is the link : [enter link description here][1]

just enable less secure apps : (Allow less secure apps: ON)

the correct way to import is : from flask_mail import Mail, Message

everything should be fine now cheers