Skip to main content

Official API client

Project description

Official Mailtrap Python client

This Python package offers integration with the official API for Mailtrap.

Quickly add email sending functionality to your Python application with Mailtrap.

Compatibility with previous releases

Versions of this package up to 1.0.1 were a different, unrelated project, that is now maintained as Sendria. To continue using it, see instructions.



  • Python version 3.6+

Install package

pip install mailtrap



import mailtrap as mt

# create mail object
mail = mt.Mail(
    sender=mt.Address(email="", name="Mailtrap Test"),
    subject="You are awesome!",
    text="Congrats for sending test email with Mailtrap!",

# create client and send
client = mt.MailtrapClient(token="your-api-key")


import base64
from pathlib import Path

import mailtrap as mt

welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes()

mail = mt.Mail(
    sender=mt.Address(email="", name="Mailtrap Test"),
    to=[mt.Address(email="", name="Your name")],
    cc=[mt.Address(email="", name="Copy to")],
    bcc=[mt.Address(email="", name="Hidden Recipient")],
    subject="You are awesome!",
    text="Congrats for sending test email with Mailtrap!",
    <!doctype html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <body style="font-family: sans-serif;">
        <div style="display: block; margin: auto; max-width: 600px;" class="main">
          <h1 style="font-size: 18px; font-weight: bold; margin-top: 20px">
            Congrats for sending test email with Mailtrap!
          <p>Inspect it using the tabs you see above and learn how this email can be improved.</p>
          <img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;">
          <p>Now send your email using our fake SMTP server and integration of your choice!</p>
          <p>Good luck! Hope it works.</p>
        <!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
          .main { background-color: white; }
          a:hover { border-left-width: 1em; min-height: 2em; }
    headers={"X-MT-Header": "Custom header"},
    custom_variables={"year": 2023},

client = mt.MailtrapClient(token="your-api-key")

Using email template

import mailtrap as mt

# create mail object
mail = mt.MailFromTemplate(
    sender=mt.Address(email="", name="Mailtrap Test"),
    template_variables={"user_name": "John Doe"},

# create client and send
client = mt.MailtrapClient(token="your-api-key")


Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

Development Environment

Clone the repo
cd mailtrap-python

Install tox

tox is an environment orchestrator. We use it to setup local environments, run tests and execute linters.

python -m pip install --user tox
python -m tox --help

To setup virtual environments, run tests and linters use:


It will create virtual environments with all installed dependencies for each available python interpreter (starting from python3.6) on your machine. By default, they will be available in {project}/.tox/ directory. So, for instance, to activate python3.11 environment, run the following:

source .tox/py311/bin/activate

Information for version 1 users

If you are a version 1 user, it is advised that you upgrade to Sendria, which is the same package, but under a new name, and with new features. However, you can also continue using the last v1 release by locking the version in pip:

# To use the FORMER version of the mailtrap package, now known as Sendria:
pip install --force-reinstall -v "mailtrap==1.0.1"


The project is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Mailtrap project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct

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

mailtrap-2.0.1.tar.gz (10.0 kB view hashes)

Uploaded Source

Built Distribution

mailtrap-2.0.1-py3-none-any.whl (10.1 kB view hashes)

Uploaded Python 3

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