Skip to main content

Send mail with mailclerk.app

Project description

Mailclerk Logo

Mailclerk Python

Mailclerk helps anyone on your team design great emails, improve their performance, and free up developer time. Learn more

Table of Contents

Requirements

  1. Python 2.7+ or Python 3.4+

Setup

To install, run:

pip install mailclerk

API Key & URL

To set the Mailclerk API Key (begins with mc_), you can provide it as an environmental variable: MAILCLERK_API_KEY. Alternatively, you can set it directly on the Mailclerk module:

import mailclerk
mailclerk.api_key = "mc_yourprivatekey"

If you are using version control like git, we strongly recommend storing your production API keys in environmental variables.

The default API endpoint is https://api.mailclerk.app. To change this, you can provide a MAILCLERK_API_URL ENV variable or set mailclerk.mailclerk_url.

Usage

You'll need an active account and at least one template (in the example welcome-email).

To send an email to "alice@example.com":

mailclerk.deliver("welcome-email", "alice@example.com")

If the template has any dynamic data, you can include it in the third parameter as a hash:

mailclerk.deliver("welcome-email", "alice@example.com", { name: "Alice" })

See Mailclerk documentation for more details.

Testing

Your Mailclerk environment has two API keys: a production key (beginning with mc_live) and a test key (beginning with mc_test). If you use the test key, emails will not be delivered, but will show up in the logs on your Mailclerk account and can be previewed there. This replaces tools like naomi for previewing emails in development.

To avoid cluttering up your Mailclerk test logs with sends triggered by your automated test suite, call mailclerk.outbox.enable() in the file that configures your tests. For example, in Django, add it to the test environment-specific section of your settings.py file.

This will also enable utility methods which you can use to write tests that check emails are sent with the correct data:

# Number of emails "sent"
len(mailclerk.outbox)

# Returns all emails of matching a template or email recipient. See method
mailclerk.outbox.filter(template="welcome-email")
mailclerk.outbox.filter(recipient_email="gilles@example.com")

# Returns the most recent email:
email = mailclerk.outbox[-1]
email.template        # "welcome-email"
email.recipient_email # "gilles@example.com"
email.subject         # "Welcome to Acme, Gilles"
email.html            # "<html><body>..."

In between test cases (for example, the setUp() method of a unittest case), you should clear the stored emails by calling mailclerk.outbox.reset().

The emails have the following attributes:

Attribute Description
template Slug of the template sent (1st argument to mailclerk.deliver)
recipient Hash representing the send recipient (2nd argument to mailclerk.deliver)
recipient_email Email of the send recipient
recipient_name Name of the send recipient (nil if not specified)
data Dynamic data for the send (3rd argument to mailclerk.deliver)
options Options specified for the send (4th argument to mailclerk.deliver)
from From Mailclerk: Hash with name and address of the sender
subject From Mailclerk: Text of the send's subject line
preheader From Mailclerk: Text of the send's preheader
html From Mailclerk: Rendered body HTML for the send
text From Mailclerk: Rendered plaintext version of the send
headers From Mailclerk: Extra email headers (e.g. reply-to)

See the Mailclerk testing documentation for more details.

Varying API Keys

If you need to use multiple API keys, you can also initialize mailclerk.MailclerkAPIClient instances with different keys. This:

mc_client = mailclerk.MailclerkAPIClient("mc_yourprivatekey")
mc_client.deliver("welcome-email", "bob@example.com")

Is equivalent to this:

mailclerk.api_key = "mc_yourprivatekey"
mailclerk.deliver("welcome-email", "bob@example.com")

Package Tests

Install test dependencies:

pip install .[test]

Run with:

python -m unittest discover

Versioning

Read Semantic Versioning for details. Briefly, it means:

  • Major (X.y.z) - Incremented for any backwards incompatible public API changes.
  • Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
  • Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.

Code of Conduct

Please note that this project is released with a CODE OF CONDUCT. By participating in this project you agree to abide by its terms.

Contributions

Read CONTRIBUTING for details.

License

Copyright 2021 Mailclerk. Read LICENSE for details.

History

Read CHANGES for details. Built with Gemsmith.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mailclerk-1.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

mailclerk-1.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file mailclerk-1.1.0.tar.gz.

File metadata

  • Download URL: mailclerk-1.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.6.5

File hashes

Hashes for mailclerk-1.1.0.tar.gz
Algorithm Hash digest
SHA256 03a699414e8afd790271049951889d2e0be4ab2570f933540594f707a44592e7
MD5 3225b1b87e59f7be9c2d2b519f83294a
BLAKE2b-256 b439068fb2509e0cb660f7b473a510001b5c5e78018297181d07493ca1926ea6

See more details on using hashes here.

File details

Details for the file mailclerk-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: mailclerk-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.6.5

File hashes

Hashes for mailclerk-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78e9a90c6a38c3cd7806954445f225c7771ab94912e4484abb1b028670856c18
MD5 9a53040d35bbe3c27f898b55a17b032f
BLAKE2b-256 8b6ac05344788012ea780a45c3c4ba0a5de828d2d68ab94bd223967aad4919cc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page