Forums

flask_mail not sending emails

I'm having some trouble when sending emails from my webapp that I'm hoping you can help me with. I do not encounter these errors on my development machine. I can send emails and they are received with no issues in the same way, using the same credentials.

:

I've narrowed this down to the fact the email is sent using current_app.executor.submit (ie as a background task). When I remove that and send as part of the function, the mail gets sent no problem. Does anyone know how I can do this and not have to wait for the email to be sent before rendering the page?

Unfortunately website code on PythonAnywhere runs single-threaded, so you'll need to do it in the view. It shouldn't take that long, though -- sending an email would normally take tenths of a second a most.

That is a shame. Do you know of any other way I can achieve something similar? The performance impact of sending a mail and sending a text before the page renders is not acceptable. I read on another forum blog that Django-Q is a way round this but I'm using flask so no good to me.

Unfortunately you'd need to set up your own task queue to do that -- that is, write something to a database, and have an always-on task poll that database for async tasks to perform. This help page as a bit more detail on that architectural pattern.

hmmmm ok thanks. I've got a flask implementation so that reference is no good for me - thanks though. i might need to rely on a javascript fetch to do what I need done post the load

I don't think the "task queue" solution has framework constraints, but of course choose a solution that works best for you!