Skip to content

Instantly share code, notes, and snippets.

@ondoheer
Created April 27, 2015 03:25
Show Gist options
  • Select an option

  • Save ondoheer/29d0c0f8c36a25da1fe8 to your computer and use it in GitHub Desktop.

Select an option

Save ondoheer/29d0c0f8c36a25da1fe8 to your computer and use it in GitHub Desktop.
Trouble sending mail asynchronously
@main.route("/mail/<int:_id>/<int:number>")
@login_required
def mailSession(_id, number):
from flask_mail import Message
from decorators import async
from app import mail, create_app
@async
def send_async_email(app, msg):
with app.app_context():
mail.send(msg)
def send_email(subject, sender, recipients, text_body, html_body):
msg = Message(subject,
sender=sender,
recipients=recipients
)
msg.body = text_body
msg.html = html_body
send_async_email(create_app("default"), msg)
stp_id = user.get_id()
therapist = users_db.find_one({"stp_id": stp_id})
patient = therapist["patients"][str(_id)]
sessions = patient["sessions"]
session = sessions[str(number)]
mail_body = render_template(
'sessionEmail.txt',
user=user,
number=number,
patient=patient,
session=session
)
mail_html = render_template(
'sessionEmail.html',
user=user,
number=number,
patient=patient,
session=session
)
send_email(
"Sesión de Flores de Bach",
sender=therapist["name"],
recipients=[patient["email"]],
text_body=mail_body,
html_body=mail_html
)
return redirect(url_for('main.showSession', _id=_id, number=number))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment