|
| 1 | +# Official Mailtrap Python client |
| 2 | + |
| 3 | +This Python package offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io). |
| 4 | + |
| 5 | +Quickly add email sending functionality to your Python application with Mailtrap. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +### Prerequisites |
| 10 | + |
| 11 | +- Python version 3.6+ |
| 12 | + |
| 13 | +### Install package |
| 14 | + |
| 15 | +```text |
| 16 | +pip install mailtrap |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Minimal |
| 22 | + |
| 23 | +```python |
| 24 | +import mailtrap as mt |
| 25 | + |
| 26 | +# create mail object |
| 27 | +mail = mt.Mail( |
| 28 | + sender=mt.Address( email="[email protected]", name="Mailtrap Test"), |
| 29 | + to=[mt.Address( email="[email protected]")], |
| 30 | + subject="You are awesome!", |
| 31 | + text="Congrats for sending test email with Mailtrap!", |
| 32 | +) |
| 33 | + |
| 34 | +# create client and send |
| 35 | +client = mt.MailtrapClient(token="your-api-key") |
| 36 | +client.send(mail) |
| 37 | +``` |
| 38 | + |
| 39 | +### Full |
| 40 | + |
| 41 | +```python |
| 42 | +import base64 |
| 43 | +from pathlib import Path |
| 44 | + |
| 45 | +import mailtrap as mt |
| 46 | + |
| 47 | +welcome_image = Path(__file__).parent.joinpath("welcome.png").read_bytes() |
| 48 | + |
| 49 | +mail = mt.Mail( |
| 50 | + sender=mt.Address( email="[email protected]", name="Mailtrap Test"), |
| 51 | + to=[mt.Address( email="[email protected]", name="Your name")], |
| 52 | + cc=[mt.Address( email="[email protected]", name="Copy to")], |
| 53 | + bcc=[mt.Address( email="[email protected]", name="Hidden Recipient")], |
| 54 | + subject="You are awesome!", |
| 55 | + text="Congrats for sending test email with Mailtrap!", |
| 56 | + html=""" |
| 57 | + <!doctype html> |
| 58 | + <html> |
| 59 | + <head> |
| 60 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 61 | + </head> |
| 62 | + <body style="font-family: sans-serif;"> |
| 63 | + <div style="display: block; margin: auto; max-width: 600px;" class="main"> |
| 64 | + <h1 style="font-size: 18px; font-weight: bold; margin-top: 20px"> |
| 65 | + Congrats for sending test email with Mailtrap! |
| 66 | + </h1> |
| 67 | + <p>Inspect it using the tabs you see above and learn how this email can be improved.</p> |
| 68 | + <img alt="Inspect with Tabs" src="cid:welcome.png" style="width: 100%;"> |
| 69 | + <p>Now send your email using our fake SMTP server and integration of your choice!</p> |
| 70 | + <p>Good luck! Hope it works.</p> |
| 71 | + </div> |
| 72 | + <!-- Example of invalid for email html/css, will be detected by Mailtrap: --> |
| 73 | + <style> |
| 74 | + .main { background-color: white; } |
| 75 | + a:hover { border-left-width: 1em; min-height: 2em; } |
| 76 | + </style> |
| 77 | + </body> |
| 78 | + </html> |
| 79 | + """, |
| 80 | + category="Test", |
| 81 | + attachments=[ |
| 82 | + mt.Attachment( |
| 83 | + content=base64.b64encode(welcome_image), |
| 84 | + filename="welcome.png", |
| 85 | + disposition=mt.Disposition.INLINE, |
| 86 | + mimetype="image/png", |
| 87 | + content_id="welcome.png", |
| 88 | + ) |
| 89 | + ], |
| 90 | + headers={"X-MT-Header": "Custom header"}, |
| 91 | + custom_variables={"year": 2023}, |
| 92 | +) |
| 93 | + |
| 94 | +client = mt.MailtrapClient(token="your-api-key") |
| 95 | +client.send(mail) |
| 96 | +``` |
| 97 | + |
| 98 | +### Using email template |
| 99 | + |
| 100 | + |
| 101 | +```python |
| 102 | +import mailtrap as mt |
| 103 | + |
| 104 | +# create mail object |
| 105 | +mail = mt.MailFromTemplate( |
| 106 | + sender=mt.Address( email="[email protected]", name="Mailtrap Test"), |
| 107 | + to=[mt.Address( email="[email protected]")], |
| 108 | + template_uuid="2f45b0aa-bbed-432f-95e4-e145e1965ba2", |
| 109 | + template_variables={"user_name": "John Doe"}, |
| 110 | +) |
| 111 | + |
| 112 | +# create client and send |
| 113 | +client = mt.MailtrapClient(token="your-api-key") |
| 114 | +client.send(mail) |
| 115 | +``` |
| 116 | + |
| 117 | +## Contributing |
| 118 | + |
| 119 | +Bug reports and pull requests are welcome on [GitHub](https://github.com/railsware/mailtrap-python-sdk). 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). |
| 120 | + |
| 121 | +### Development Environment |
| 122 | + |
| 123 | +#### Clone the repo |
| 124 | + |
| 125 | +```bash |
| 126 | +https://github.com/railsware/mailtrap-python-sdk.git |
| 127 | +cd mailtrap-python-sdk |
| 128 | +``` |
| 129 | + |
| 130 | +#### Install [tox](https://tox.wiki/en/latest/installation.html) |
| 131 | +`tox` is an environment orchestrator. We use it to setup local environments, run tests and execute linters. |
| 132 | +```bash |
| 133 | +python -m pip install --user tox |
| 134 | +python -m tox --help |
| 135 | +``` |
| 136 | + |
| 137 | +To setup virtual environments, run tests and linters use: |
| 138 | +```bash |
| 139 | +tox |
| 140 | +``` |
| 141 | +It will create virtual environments with all installed dependencies for each available python interpreter (starting from `python3.6`) on your machine. |
| 142 | +By default, they will be available in `{project}/.tox/` directory. So, for instance, to activate `python3.11` environment, run the following: |
| 143 | +```bash |
| 144 | +source .tox/py311/bin/activate |
| 145 | +``` |
| 146 | + |
| 147 | + |
| 148 | +## License |
| 149 | + |
| 150 | +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). |
| 151 | + |
| 152 | +## Code of Conduct |
| 153 | + |
| 154 | +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) |
0 commit comments