Skip to main content

The Mailosaur Python library lets you integrate email and SMS testing into your continuous integration process.

Project description

Mailosaur - Python library ·

Mailosaur lets you automate email and SMS tests as part of software development and QA.

  • Unlimited test email addresses for all - every account gives users an unlimited number of test email addresses to test with.
  • End-to-end (e2e) email and SMS testing Allowing you to set up end-to-end tests for password reset emails, account verification processes and MFA/one-time passcodes sent via text message.
  • Fake SMTP servers Mailosaur also provides dummy SMTP servers to test with; allowing you to catch email in staging environments - preventing email being sent to customers by mistake.

Get Started

This guide provides several key sections:

You can find the full Mailosaur documentation on the website.

If you get stuck, just contact us at support@mailosaur.com.

Installation

pip install --upgrade mailosaur

Set your API key

Get your API key from the Mailosaur Dashboard and set it as an environment variable:

export MAILOSAUR_API_KEY='your-api-key-here'

Create your code

Then import the library and create a client:

from mailosaur import MailosaurClient
mailosaur = MailosaurClient()

API Reference

This library is powered by the Mailosaur email & SMS testing API. You can easily check out the API itself by looking at our API reference documentation or via our Postman or Insomnia collections:

Run in Postman Run in Insomnia}

Creating an account

Create a free trial account for Mailosaur via the website.

Once you have this, navigate to the API tab to find the following values:

  • Server ID - Servers act like projects, which group your tests together. You need this ID whenever you interact with a server via the API.
  • Server Domain - Every server has its own domain name. You'll need this to send email to your server.
  • API Key - You can create an API key per server (recommended), or an account-level API key to use across your whole account. Learn more about API keys.

Test email addresses with Mailosaur

Mailosaur gives you an unlimited number of test email addresses - with no setup or coding required!

Here's how it works:

  • When you create an account, you are given a server.
  • Every server has its own Server Domain name (e.g. abc123.mailosaur.net)
  • Any email address that ends with @{YOUR_SERVER_DOMAIN} will work with Mailosaur without any special setup. For example:
    • build-423@abc123.mailosaur.net
    • john.smith@abc123.mailosaur.net
    • rAnDoM63423@abc123.mailosaur.net
  • You can create more servers when you need them. Each one will have its own domain name.

Can't use test email addresses? You can also use SMTP to test email. By connecting your product or website to Mailosaur via SMTP, Mailosaur will catch all email your application sends, regardless of the email address.

Find an email

In automated tests you will want to wait for a new email to arrive. This library makes that easy with the messages.get method. Here's how you use it:

from mailosaur import MailosaurClient
from mailosaur.models import SearchCriteria

mailosaur = MailosaurClient()

# See https://mailosaur.com/app/project/api
server_id = "abc123"
server_domain = "abc123.mailosaur.net"

criteria = SearchCriteria()
criteria.sent_to = "anything@" + server_domain

email = mailosaur.messages.get(server_id, criteria)

print(email.subject) # "Hello world!"

What is this code doing?

  1. Sets up an instance of MailosaurClient using the MAILOSAUR_API_KEY environment variable.
  2. Waits for an email to arrive at the server with ID abc123.
  3. Outputs the subject line of the email.

Find an SMS message

Important: Trial accounts do not automatically have SMS access. Please contact our support team to enable a trial of SMS functionality.

If your account has SMS testing enabled, you can reserve phone numbers to test with, then use the Mailosaur API in a very similar way to when testing email:

from mailosaur import MailosaurClient
from mailosaur.models import SearchCriteria

mailosaur = MailosaurClient()

# See https://mailosaur.com/app/project/api
server_id = "abc123"
server_domain = "abc123.mailosaur.net"

criteria = SearchCriteria()
criteria.sent_to = "4471235554444"

sms = mailosaur.messages.get(server_id, criteria)

print(sms.text.body)

Testing plain text content

Most emails, and all SMS messages, should have a plain text body. Mailosaur exposes this content via the text.body property on an email or SMS message:

print(message.text.body) # "Hi Jason, ..."

if "Jason" in message.text.body:
  print('Email contains "Jason"')

Extracting verification codes from plain text

You may have an email or SMS message that contains an account verification code, or some other one-time passcode. You can extract content like this using a simple regex.

Here is how to extract a 6-digit numeric code:

print(message.text.body) # "Your access code is 243546."

match = re.search("([0-9]{6})", message.text.body)
print(match.group()) # "243546"

Read more

Testing HTML content

Most emails also have an HTML body, as well as the plain text content. You can access HTML content in a very similar way to plain text:

print(message.html.body) # "<html><head ..."

Working with HTML using Beautiful Soup

If you need to traverse the HTML content of an email. For example, finding an element via a CSS selector, you can use the Beautiful Soup library.

pip install beautifulsoup4
from bs4 import BeautifulSoup

# ...

dom = BeautifulSoup(message.html.body, 'html.parser')

el = dom.find('.verification-code')
verification_code = el.text

print(verification_code) # "542163"

Read more

Working with hyperlinks

When an email is sent with an HTML body, Mailosaur automatically extracts any hyperlinks found within anchor (<a>) and area (<area>) elements and makes these viable via the html.links array.

Each link has a text property, representing the display text of the hyperlink within the body, and an href property containing the target URL:

# How many links?
print(len(message.html.links)) # 2

first_link = message.html.links[0]
print(first_link.text) # "Google Search"
print(first_link.href) # "https://www.google.com/"

Important: To ensure you always have valid emails. Mailosaur only extracts links that have been correctly marked up with <a> or <area> tags.

Links in plain text (including SMS messages)

Mailosaur auto-detects links in plain text content too, which is especially useful for SMS testing:

# How many links?
print(len(message.text.links)) # 2

first_link = message.text.links[0]
print(first_link.href) # "https://www.google.com/"

Working with attachments

If your email includes attachments, you can access these via the attachments property:

# How many attachments?
print(len(message.attachments)) # 2

Each attachment contains metadata on the file name and content type:

first_attachment = message.attachments[0]
print(first_attachment.file_name) # "contract.pdf"
print(first_attachment.content_type) # "application/pdf"

The length property returns the size of the attached file (in bytes):

first_attachment = message.attachments[0]
print(first_attachment.length) # 4028

Working with images and web beacons

The html.images property of a message contains an array of images found within the HTML content of an email. The length of this array corresponds to the number of images found within an email:

# How many images in the email?
print(len(message.html.images)) # 1

Remotely-hosted images

Emails will often contain many images that are hosted elsewhere, such as on your website or product. It is recommended to check that these images are accessible by your recipients.

All images should have an alternative text description, which can be checked using the alt attribute.

image = message.html.images[0]
print(image.alt) # "Hot air balloon"

Triggering web beacons

A web beacon is a small image that can be used to track whether an email has been opened by a recipient.

Because a web beacon is simply another form of remotely-hosted image, you can use the src attribute to perform an HTTP request to that address:

import requests

# ...

const image = message.html.images[0]
print(image.src) # "https://example.com/s.png?abc123"

# Make an HTTP call to trigger the web beacon
response = requests.get(image.src)
print(response.status_code) # 200

Spam checking

You can perform a SpamAssassin check against an email. The structure returned matches the spam test object:

result = mailosaur.analysis.spam(message.id)

print(result.score) # 0.5

for r in result.spam_filter_results.spam_assassin:
  print(r.rule)
  print(r.description)
  print(r.score)

Development

You must have the following prerequisites installed:

Install all development dependencies:

pip install -r requirements.txt

The test suite requires the following environment variables to be set:

export MAILOSAUR_BASE_URL=https://mailosaur.com/
export MAILOSAUR_API_KEY=your_api_key
export MAILOSAUR_SERVER=server_id

Run all tests:

pytest

Contacting us

You can get us at support@mailosaur.com

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

mailosaur-8.1.0.tar.gz (30.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mailosaur-8.1.0-py2.py3-none-any.whl (46.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file mailosaur-8.1.0.tar.gz.

File metadata

  • Download URL: mailosaur-8.1.0.tar.gz
  • Upload date:
  • Size: 30.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for mailosaur-8.1.0.tar.gz
Algorithm Hash digest
SHA256 4e2b3cf1e390eac37a0629490de0dbd8f81ba51580424e232125be95aaf68582
MD5 cf8cef15fa77ff3a3cc9e528525f682f
BLAKE2b-256 d9fb59d77c23303400d2f78154ae82295f75e04265a145c7a4a14bd8034525f5

See more details on using hashes here.

File details

Details for the file mailosaur-8.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: mailosaur-8.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 46.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for mailosaur-8.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4891c3ca369db40b6f35dcd60405b72e583b4450a3e24e8bbf8179b20e5eba3b
MD5 fe79ea2533ad22a9dcb5aab5902179d7
BLAKE2b-256 8ce12722a811fb37b427d0fa002860ec75560c9d301709b32ab1ca5d5b991662

See more details on using hashes here.

Supported by

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