Unofficial Async fork of the official Mailtrap Python client
Adds async support to the official Mailtrap Python client. This fork is not maintained by the original authors of the 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
The fork requires Python 3.10 and uses httpx rather than requests.
The non-async version of the official Mailtrap Python client is available at mailtrap.
This version maintains the same API as the official Mailtrap Python client, with the addition of an async send method, it is called using await client.asend.
I have versioned this as 3.0.0 due to the breaking changes in the dependencies.
Original compatilibility message
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.
Installation
Prerequisites
- Python version 3.10+
Install package
pip install mailtrap
Usage
Minimal
import mailtrap as mt
# create mail object
mail = mt.Mail(
sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"),
to=[mt.Address(email="your@email.com")],
subject="You are awesome!",
text="Congrats for sending test email with Mailtrap!",
)
# create client and send
client = mt.MailtrapClient(token="your-api-key")
client.send(mail)
Async
mail = mt.Mail(
sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"),
to=[mt.Address(email="your@email.com")],
subject="You are awesome!",
text="Congrats for sending test email with Mailtrap!",
)
# create client and send
client = mt.MailtrapClient(token="your-api-key")
await client.asend(mail)
Full
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="mailtrap@example.com", name="Mailtrap Test"),
to=[mt.Address(email="your@email.com", name="Your name")],
cc=[mt.Address(email="cc@email.com", name="Copy to")],
bcc=[mt.Address(email="bcc@email.com", name="Hidden Recipient")],
subject="You are awesome!",
text="Congrats for sending test email with Mailtrap!",
html="""
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<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!
</h1>
<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>
</div>
<!-- Example of invalid for email html/css, will be detected by Mailtrap: -->
<style>
.main { background-color: white; }
a:hover { border-left-width: 1em; min-height: 2em; }
</style>
</body>
</html>
""",
category="Test",
attachments=[
mt.Attachment(
content=base64.b64encode(welcome_image),
filename="welcome.png",
disposition=mt.Disposition.INLINE,
mimetype="image/png",
content_id="welcome.png",
)
],
headers={"X-MT-Header": "Custom header"},
custom_variables={"year": 2023},
)
client = mt.MailtrapClient(token="your-api-key")
client.send(mail)
Using email template
import mailtrap as mt
# create mail object
mail = mt.MailFromTemplate(
sender=mt.Address(email="mailtrap@example.com", name="Mailtrap Test"),
to=[mt.Address(email="your@email.com")],
template_uuid="2f45b0aa-bbed-432f-95e4-e145e1965ba2",
template_variables={"user_name": "John Doe"},
)
# create client and send
client = mt.MailtrapClient(token="your-api-key")
client.send(mail)
Contributing
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
https://github.com/railsware/mailtrap-python.git
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:
tox
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"
License
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
Metadata
Release files for mailtrap-unofficial-async 3.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mailtrap_unofficial_async-3.0.0.tar.gz | 10.8 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mailtrap_unofficial_async-3.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.6 kB
Release files / mailtrap_unofficial_async-3.0.0.tar.gz
| Download URL | mailtrap_unofficial_async-3.0.0.tar.gz |
|---|---|
| Size | 10.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa747eb9190a28fe20d1f814353cf28cd1af8a1eb8fafde3d955c289b5705d37
|
|
BLAKE2b-256 checksum How to use checksums |
3f5c7a99bd878d972bb69a4269c44eeda68259ebfcf7bc922d2d38f2338fb754
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 7, 2025.
Transparency logRelease files / mailtrap_unofficial_async-3.0.0-py3-none-any.whl
| Download URL | mailtrap_unofficial_async-3.0.0-py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
35128370fd3d5dc62cb29a6a089d6e85b2782e047fe785b5a044e73977f1e642
|
|
BLAKE2b-256 checksum How to use checksums |
1ef0dddd127344bf28911e37e7b9dd0f4f81a0f20aa81aa196746e52394e0c03
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 7, 2025.
Transparency logSecurity for mailtrap-unofficial-async 3.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for mailtrap_unofficial_async-3.0.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 7, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.