Flask-Email

Flask-Email is a fork of the core email backends in Django to be used with Flask. In addition there are extra backends that might be useful.

Documentation: https://flask-email.readthedocs.org/en/latest/

TODO: Finish documentation

Installion

Install with pip:

pip install Flask-Email

or install with easy_install:

easy_install Flask-Email

Usage

Instantiate a email backend:

from flask import Flask
from flask.ext.email import ConsoleMail

app = Flask(__name__)
mailbox = ConsoleMail(app)

or initialize it later init_app():

mailbox = ConsoleMail()

def create_app(config='settings.cfg'):
    app = Flask(__name__)
    app.config.from_pyfile(config)
    mailbox.init_app(app)
    return app

Sending an Email

email = EmailMessage(
    'Subject',
    'Content',
    'bounce@example.com',
    ['to@example.com'],
    headers={'From': 'from@example.com'}
)
email.send(mailbox)

Shortcuts

send_mail('Subject', 'Content', 'bounce@example.com', ['to@example.com'])

Configuration

Flask-Email accepts the following settings regardless of email backend

DEFAULT_CHARSET

Default charset to use for all EmailMessage.

Defaults to 'utf-8'

DEFAULT_FROM_EMAIL

Default email address to use for when the sender parameter not present.

Defaults to 'webmaster@localhost'

SERVER_EMAIL

The email address that messages come from when using mail_admins() and mail_managers() and no sender is provided.

Defaults to 'root@localhost'

EMAIL_SUBJECT_PREFIX

Subject-line prefix for email messages sent with mail_admins() or mail_managers(). You’ll probably want to include the trailing space.

Defaults to '[Flask] '

ADMINS

A tuple following one of the below formats that specifies who should receive messages from mail_admins().

Tuple of name and email:

(('John', 'john@example.com'), ('Mary', 'mary@example.com'))

Email format name <email>:

('John <john@example.com>', 'Mary <mary@example.com>')

Defaults to () (Empty tuple)

MANAGERS

A tuple in the same format as ADMINS that specifies who should receive messages from mail_managers()

Defaults to () (Empty tuple)

EMAIL_BACKEND

The backend to use for sending emails.

Defaults to 'flask.ext.email.backends.locmem.Mail'

Email Backends

SMTPMail

EMAIL_HOST

The host to use for sending email.

Defaults to 'localhost'

EMAIL_PORT

Port to use for the SMTP server defined in EMAIL_HOST.

Defaults to 25

EMAIL_HOST_USER

Username to use for the SMTP server defined in EMAIL_HOST. If empty, authentication will be skipped.

Defaults to '' (Empty string)

EMAIL_HOST_PASSWORD

Password to use for the SMTP server defined in EMAIL_HOST. This setting is used in conjunction with EMAIL_HOST_USER when authenticating to the SMTP server. If either of these settings is empty, authentication will be skipped.

Defaults to '' (Empty string)

EMAIL_USE_TLS

Whether to use a TLS (secure) connection when talking to the SMTP server.

Defaults to False

EMAIL_USE_SSL

Whether to use a TLS (secure) connection when talking to the SMTP server.

Defaults to False

FilebasedMail

EMAIL_FILE_PATH

The directory used by the file email backend to store output files.

Defaults to None

ConsoleMail

DummyMail

LocmemMail

API

Extend

Changelog

Version 1.4.3

Released Jan 9, 2013

  • Initial port of Django 1.4.3

Notes