Flask Snippets

Snippets are unofficial and unmaintained.

This is an archived view of user-submitted snippets. Despite being hosted on the Flask site, they are not official. No Flask maintainer has curated or checked the snippets for security, correctness, or design.

Using TurboMail with Flask

Posted by Dan Jacob on 2010-05-08 @ 14:20 and filed in Utilities

TurboMail is an email package originally bundled with TurboGears but now existing as a standalone library.

It provides convenient functionality for configuring and sending emails, for example for testing, multipart messages, etc.

It's quite straightforward to set up TurboMail for your Flask application. One thing you need to take care of is ensuring that the interface is set up so that when the current process exits, any unsent emails are cleanly dispatched. This is achieved using the standard atexit module.

import atexit

from turbomail.control import interface
from turbomail.message import Message

from flask import Flask

# pass in dict of config options

interface.start({'mail.on' : True})

# ensures interface cleanly shutdown when process exits

atexit.register(interface.stop, force=True)

app = Flask(__name__)

def index():
    # send an email
    msg = Message("from@example.com",
                  "a subject")
    msg.plain = "body of message"

This snippet by Dan Jacob can be used freely for anything you like. Consider it public domain.