Skip to main content

apistar-mail

PyPI PyPI Build Status codecov

Provides a simple interface to set up SMTP with your APIStar application and send messages from your view functions. Please note this work derives largely from the Flask-Mail extension by 'Dan Jacob' and contributors, but has been modified extensively to remove Python 2 support and be used as an APIStar component.

Installation

$ pip install apistar-mail

Usage

Example Setup

To send mail messages from your view functions you must include a dictionary of mail options to the MailComponent. Here we have a minimally viable app capable of sending an email message and returning a 204 response code:

from apistar import App, Route
from apistar.http import Response
from apistar_mail import MailComponent, Mail, Message

mail_options = {
    'MAIL_SERVER': 'smtp.example.com',
    'MAIL_USERNAME': 'me@example.com',
    'MAIL_PASSWORD': 'dontcommitthistoversioncontrol',
    'MAIL_PORT': 587,
    'MAIL_USE_TLS': True,
    'MAIL_DEFAULT_SENDER': 'me@example.com'
}


def send_a_message(mail: Mail):
    msg = Message(subject='Hello',
                  body='Welcome to APIStar!',
                  recipients=['you@example.com'])
    mail.send(msg)
    return Response('', 204)


routes = [
    Route('/', 'POST', send_a_message)
]

components = [
    MailComponent(**mail_options)
]

app = App(
    routes=routes,
    components=components
)

if __name__ == '__main__':
    app.serve('127.0.0.1', 5000, debug=True)

Sending Messages

To send a message ,first include the Mail component for injection into your view. Then create an instance of Message, and pass it to your Mail component using mail.send(msg)

from apistar_mail import Mail, Message

def send_a_message(mail:Mail):
    msg = Message('Hello',
                  sender='drew@example.com',
                  recipients=['you@example.com'])
    mail.send(msg)
    return

Your message recipients can be set in bulk or individually:

msg.recipients = ['you@example.com', 'me@example.com']
msg.add_recipient('otherperson@example.com')

If you have set MAIL_DEFAULT_SENDER you don’t need to set the message sender explicitly, as it will use this configuration value by default:

msg = Message('Hello',
              recipients=['you@example.com'])

The sender can also be passed as a two element tuple containing a name and email address which will be split like so:

msg = Message('Hello',
              sender=('Me', 'me@example.com'))

assert msg.sender == 'Me <me@example.com>'

A Message can contain a body and/or HTML:

msg.body = 'message body'
msg.html = '<b>Hello apistar_mail!</b>'

Configuration Options

apistar-mail is configured through the inclusion of the MAIL dictionary in your apistar settings. These are the available options:

  • 'MAIL_SERVER': default 'localhost'
  • 'MAIL_USERNAME': default None
  • 'MAIL_PASSWORD': default None
  • 'MAIL_PORT': default 25
  • 'MAIL_USE_TLS': default False
  • 'MAIL_USE_SSL': default False
  • 'MAIL_DEFAULT_SENDER': default None
  • 'MAIL_DEBUG': default False
  • 'MAIL_MAX_EMAILS': default None
  • 'MAIL_SUPPRESS_SEND': default False
  • 'MAIL_ASCII_ATTACHMENTS': False

Testing

To run the test suite with coverage first install the package in editable mode with it's testing requirements:

$ pip install -e ".[testing]"

To run the project's tests

$ pytest --cov

To run tests against multiple python interpreters use:

$ tox

HISTORY

0.3.0 Added support for APIStar version >= 0.4.0

0.2.1 Pinned APIStar requirement to 0.3.9

APIStar 0.4.0 introduced breaking changes to the framework. Since Components have completely changed we are pinning the requirement for this release.

Release files for apistar-mail 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for apistar-mail 0.3.0
File Size Uploaded
apistar-mail-0.3.0.tar.gz 13.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for apistar-mail 0.3.0
File Interpreter ABI Platform
apistar_mail-0.3.0-py35-none-any.whl Python 3.5 none any Details

Total release size: 21.7 kB

Release files / apistar-mail-0.3.0.tar.gz

Download URL apistar-mail-0.3.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9c5615760401368fad9d705bcda5353b049f2c7a133ab36fd274bb746afe95c7
BLAKE2b-256 checksum
How to use checksums
c45200f2c20bffcbb25d89a9e0fed606a373a03a9ed06cb5f70204895f43960f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / apistar_mail-0.3.0-py35-none-any.whl

Download URL apistar_mail-0.3.0-py35-none-any.whl
Size 8.8 kB
Tags Python 3.5
SHA-256 checksum
How to use checksums
147797587f2421ca7e5e9c4467915a59ee74ca948c5603fd241698e42640e538
BLAKE2b-256 checksum
How to use checksums
74226400782e2a5ee650db6d1abd7c40aa8a06cff0c8adf1d97902e765378752
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page