https://github.com/railsware/mailtrap-python
Official mailtrap.io Python client
https://github.com/railsware/mailtrap-python
Last synced: 1 day ago
JSON representation
Official mailtrap.io Python client
- Host: GitHub
- URL: https://github.com/railsware/mailtrap-python
- Owner: railsware
- License: mit
- Created: 2023-02-13T16:43:00.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-07T17:11:29.000Z (8 months ago)
- Last Synced: 2025-03-28T00:41:33.336Z (19 days ago)
- Language: Python
- Homepage: https://mailtrap.io
- Size: 31.3 KB
- Stars: 38
- Watchers: 16
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- my-awesome-github-stars - railsware/mailtrap-python - Official mailtrap.io Python client (Python)
README
[](https://github.com/railsware/mailtrap-python/actions/workflows/main.yml)
[](https://pypi.org/project/mailtrap/)
[](https://pypi.org/project/mailtrap/)# Official Mailtrap Python client
This Python package offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io).
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](https://github.com/msztolcman/sendria). To continue using it, see [instructions](#information-for-version-1-users).
## Installation
### Prerequisites
- Python version 3.6+
### Install package
```text
pip install mailtrap
```## Usage
### Minimal
```python
import mailtrap as mt# create mail object
mail = mt.Mail(
sender=mt.Address(email="[email protected]", name="Mailtrap Test"),
to=[mt.Address(email="[email protected]")],
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)
```### Full
```python
import base64
from pathlib import Pathimport mailtrap as mt
welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes()
mail = mt.Mail(
sender=mt.Address(email="[email protected]", name="Mailtrap Test"),
to=[mt.Address(email="[email protected]", name="Your name")],
cc=[mt.Address(email="[email protected]", name="Copy to")],
bcc=[mt.Address(email="[email protected]", name="Hidden Recipient")],
subject="You are awesome!",
text="Congrats for sending test email with Mailtrap!",
html="""
Congrats for sending test email with Mailtrap!
Inspect it using the tabs you see above and learn how this email can be improved.
![]()
Now send your email using our fake SMTP server and integration of your choice!
Good luck! Hope it works.
.main { background-color: white; }
a:hover { border-left-width: 1em; min-height: 2em; }
""",
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
```python
import mailtrap as mt# create mail object
mail = mt.MailFromTemplate(
sender=mt.Address(email="[email protected]", name="Mailtrap Test"),
to=[mt.Address(email="[email protected]")],
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](https://github.com/railsware/mailtrap-python). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
### Development Environment
#### Clone the repo
```bash
https://github.com/railsware/mailtrap-python.git
cd mailtrap-python
```#### Install [tox](https://tox.wiki/en/latest/installation.html)
`tox` is an environment orchestrator. We use it to setup local environments, run tests and execute linters.
```bash
python -m pip install --user tox
python -m tox --help
```To setup virtual environments, run tests and linters use:
```bash
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:```bash
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](https://github.com/msztolcman/sendria), which is the same package, but under a new name, and with [new features](https://github.com/msztolcman/sendria#changelog). However, you can also continue using the last v1 release by locking the version in pip:
```sh
# 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](https://opensource.org/licenses/MIT).
## 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](CODE_OF_CONDUCT.md)