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_CHARSETDefault charset to use for all
EmailMessage.Defaults to
'utf-8'DEFAULT_FROM_EMAILDefault email address to use for when the sender parameter not present.
Defaults to
'webmaster@localhost'SERVER_EMAILThe email address that messages come from when using
mail_admins()andmail_managers()and no sender is provided.Defaults to
'root@localhost'EMAIL_SUBJECT_PREFIXSubject-line prefix for email messages sent with
mail_admins()ormail_managers(). You’ll probably want to include the trailing space.Defaults to
'[Flask] 'ADMINSA tuple following one of the below formats that specifies who should receive messages from
mail_admins().Tuple of
nameandemail:(('John', 'john@example.com'), ('Mary', 'mary@example.com'))
Email format
name <email>:('John <john@example.com>', 'Mary <mary@example.com>')
Defaults to
() (Empty tuple)MANAGERSA tuple in the same format as ADMINS that specifies who should receive messages from
mail_managers()Defaults to
() (Empty tuple)EMAIL_BACKENDThe backend to use for sending emails.
Defaults to
'flask.ext.email.backends.locmem.Mail'
Email Backends¶
SMTPMail¶
EMAIL_HOSTThe host to use for sending email.
Defaults to
'localhost'EMAIL_PORTPort to use for the SMTP server defined in EMAIL_HOST.
Defaults to
25EMAIL_HOST_USERUsername to use for the SMTP server defined in EMAIL_HOST. If empty, authentication will be skipped.
Defaults to
'' (Empty string)EMAIL_HOST_PASSWORDPassword 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_TLSWhether to use a TLS (secure) connection when talking to the SMTP server.
Defaults to
FalseEMAIL_USE_SSLWhether to use a TLS (secure) connection when talking to the SMTP server.
Defaults to
False
FilebasedMail¶
EMAIL_FILE_PATHThe directory used by the file email backend to store output files.
Defaults to
None