Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Thread to support email delivery in background #20

Closed
onlyphantom opened this issue Mar 24, 2019 · 0 comments
Closed

Use Thread to support email delivery in background #20

onlyphantom opened this issue Mar 24, 2019 · 0 comments
Assignees
Labels
enhancement Minor features or request

Comments

@onlyphantom
Copy link
Owner

Prevent the server from waiting (thus slowing down) while the email delivery (eg. Reset Password request) is being sent.

Old:

from flask_mail import Mail
def send_email(subject, sender, recipients, message):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.html = message
    mail.send() # mail = Mail(app)

New:

def send_email_bg(app, message):
    with app.app_context():
        mail.send(message)

def send_email(subject, sender, recipients, message):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.html = message
    Thread(target=send_email_bg, args=(app, msg)).start()
@onlyphantom onlyphantom added the enhancement Minor features or request label Mar 24, 2019
@onlyphantom onlyphantom self-assigned this Mar 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Minor features or request
Projects
None yet
Development

No branches or pull requests

1 participant