The flask-emails extension is a simple way to send email messages from Flask application. It is a wrapper for python-emails.
- Email message abstraction with html and text part, with inline attachments, etc.
- Email body template rendering
- Email HTML body transform methods: css inlining, image inlining, etc.
- SMTP backends
- DKIM signature
- Configured via Flask application config
Note
There are another flask extension Flask-Mail which solves almost same problems. I guess flask-emails solves little more problems.
Add email-specific options to your flask application, for example:
from flask import Flask
app = Flask(__name__)
app.config = {'EMAIL_HOST': 'localhost', 'EMAIL_PORT': 25, 'EMAIL_TIMEOUT': 10}
Create and send email:
from flask_emails import Message
message = Message(html='<html><p>Hi! ...',
subject="Party today",
mail_from=("John Brown", "[email protected]"))
message.attach(data=open('Event.ics', 'rb'), filename='Event.ics')
r = message.send(to=("Nick Jackson", "[email protected]"))
if r.status_code not in [250, ]:
# message is not sent, deal with this
...
See more examples on python-emails docs
By default flask-emails reads configuration from current Flask application config when you first time create :meth:`~Message` object.
It reads the following variables:
.. tabularcolumns:: |p{6.5cm}|p{8.5cm}|
EMAIL_HOST |
The host to use for sending email. Default: |
EMAIL_PORT |
Port to use for the SMTP server defined in Default: |
EMAIL_HOST_USER |
Username to use for the SMTP server defined in Default: empty |
EMAIL_HOST_PASSWORD |
Password to use for the SMTP server defined in Default: empty |
EMAIL_USE_TLS |
Whether to use a TLS (secure) connection when talking to the SMTP server.
This is used for explicit TLS connections, generally on port 587.
If you are experiencing hanging connections, see the implicit TLS setting Default: |
EMAIL_USE_SSL |
Whether to use an implicit TLS (secure) connection when talking to the SMTP server.
In most email documentation this type of TLS connection is referred to as SSL.
It is generally used on port 465.
If you are experiencing problems, see the explicit TLS setting Note that Default: |
EMAIL_SSL_CERTFILE |
If Default: |
EMAIL_SSL_KEYFILE |
If Note that setting Please refer to the documentation of Python’s Default: |
EMAIL_TIMEOUT |
Specifies a timeout in seconds for blocking operations like the connection attempt. Default: |
EMAIL_SMTP_DEBUG |
Be verbose on smtp commands. The same as Default: |
EMAIL_BACKEND |
The backend class to use for sending emails.
Available backends are default Default: |
Install flask-emails from pypi:
$ pip install flask-emails
- documentation: flask-emails.readthedocs.org
- python-emails: github.com/lavr/python-emails