Flask-Pystmark¶
Flask-Pystmark is a Flask extension for the Postmark API library Pystmark.
The extension contains two objects, Pystmark
and Message
. Pystmark
wraps the Simple API of the pystmark
module. Both inject Flask-Pystmark
configuration variables into the functions or objects they wrap.
See also
Flask-Pystmark supports Python 2.6, 2.7, 3.3 and PyPy.
Installation¶
$ pip install Flask-Pystmark
Configuration¶
Flask-Pystmark is configured through the standard Flask config API. Any of
them can be overridden in calls to the Pystmark methods or Message
construction.
These are the available options:
- PYSTMARK_API_KEY : Required. Your API key for postmarkapp.com
- PYSTMARK_HTTPS : default True. Use https for requests to postmarkapp.com
- PYSTMARK_TEST_API : default False. Use the Postmark test API. Note: a request is still made to postmarkapp.com, but accesses their sandbox.
- PYSTMARK_DEFAULT_SENDER : default None. Default sender email to use for outgoing messages
- PYSTMARK_DEFAULT_REPLY_TO : default None. Default reply_to email to use for outgoing messages
- PYSTMARK_DEFAULT_HEADERS : default None. Default headers to apply to outgoing messages. They must be in the format required by Postmark. Note: these are headers in the email. If you need headers in the request sent to postmarkapp.com, pass them in to the API wrappers as you would in a call to requests.request
- PYSTMARK_VERIFY_MESSAGES : default False. Apply sanity checks to all messages when created. Will raise pystmark.MessageError if it appears invalid.
Example¶
# app.py
from flask import Flask
from flask_pystmark import Pystmark, Message
from pystmark import ResponseError
app = Flask(__name__)
app.config['PYSTMARK_API_KEY'] = 'your_api_key'
app.config['PYSTMARK_DEFAULT_SENDER'] = 'admin@example.com'
pystmark = Pystmark(app)
@app.route('/')
def send():
m = Message(to='user@gmail.com', text='Welcome')
resp = pystmark.send(m)
try:
resp.raise_for_status()
except ResponseError as e:
return 'Failed to send message. Reason: {}'.format(e)
else:
return 'Sent message to {}'.format(resp.message.to)