Skip to content

Commit

Permalink
Chapter 6: Asynchronous emails (6b)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Nov 14, 2017
1 parent 86622e9 commit 4459abe
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion hello.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from threading import Thread
from flask import Flask, render_template, session, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_moment import Moment
Expand Down Expand Up @@ -52,12 +53,19 @@ def __repr__(self):
return '<User %r>' % self.username


def send_async_email(app, msg):
with app.app_context():
mail.send(msg)


def send_email(to, subject, template, **kwargs):
msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX'] + ' ' + subject,
sender=app.config['FLASKY_MAIL_SENDER'], recipients=[to])
msg.body = render_template(template + '.txt', **kwargs)
msg.html = render_template(template + '.html', **kwargs)
mail.send(msg)
thr = Thread(target=send_async_email, args=[app, msg])
thr.start()
return thr


class NameForm(FlaskForm):
Expand Down

0 comments on commit 4459abe

Please sign in to comment.