Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-Mail not sending emails, no error is being reported

Tags:

flask

All, I'm trying to setup flask-mail to send notifications to my email when a user registers. I'm getting no error messages from the script used to send the email, but nothing is actually being sent, or at least, nothing is being received.

Is there a log file which can show if an email was sent, rejected, or maybe if there was even a problem logging onto the server? How does on track this problem?

Any ideas here?

like image 499
GG_Python Avatar asked Jul 31 '13 20:07

GG_Python


People also ask

How do I add mailing feature to flask application?

Step 1 − Import Mail and Message class from flask-mail module in the code. Step 2 − Then Flask-Mail is configured as per following settings. Step 3 − Create an instance of Mail class. Step 4 − Set up a Message object in a Python function mapped by URL rule ('/').

What does flask-mail do?

flask-mail¶ One of the most basic functions in a web application is the ability to send emails to your users. The Flask-Mail extension provides a simple interface to set up SMTP with your Flask application and to send messages from your views and scripts.


3 Answers

Flask-Email use smtplib which can set debug level: https://github.com/mattupstate/flask-mail/blob/master/flask_mail.py#L139. You can set it with MAIL_DEBUG = True or DEBUG = True. Also check that MAIL_SUPPRESS_SEND = False and TESTING = False.

With debug I can see in stdout mail progress: success, fail, recipients and etc.

See details: http://pythonhosted.org/Flask-Mail/#configuring-flask-mail.

like image 106
tbicr Avatar answered Jan 02 '23 23:01

tbicr


tbicr has the most likely fix, check MAIL_SUPPRESS_SEND first.

What ended up burning me (being new to flask) is that when you instantiate your Mail() object, be sure it's after you've set your app.config values. The Mail() object doesn't go back and look at these values after the fact, so they will default to bad values. What's frustrating is that you won't see any errors when you try to send messages with the default/bad values. At least not as of my posting.

like image 39
Matt Avatar answered Jan 02 '23 22:01

Matt


I know this post is from a while ago, but I just ran into the same issue. Like @tbicr mentioned make sure that app.testing is set to False. As it states in the Flask-Maildocs here:

"If the setting TESTING is set to True, emails will be suppressed. Calling send() on your messages will not result in any messages being actually sent."

This was exactly my problem. I implemented Google reCAPTCHA into one of my forms and the app.testing was set to True so I did not have to hit the reCAPTCHA box every time. By removing the app.testing or by setting it to False, the emails were able to be sent.

like image 42
kstullich Avatar answered Jan 02 '23 21:01

kstullich