-
Notifications
You must be signed in to change notification settings - Fork 8
Fix issue #52: Update README.md using new guideline #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
691ddc0
Fix issue #52: Update README.md with new structure
d6962b9
Fix issue #52: Replace enums by string in response models
6e173c3
Fix issue #52: Add detailed examples for email sending
1fb507a
Fix issue #52: Fix typo, update sending examples
7180789
Fix issue #52: Add new way of sending mails to examples and README.md
1c13fdb
Fix issue #52: Fix typo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [](https://github.com/railsware/mailtrap-python/actions/workflows/main.yml) | ||
| [](https://github.com/mailtrap/mailtrap-python/actions/workflows/main.yml) | ||
| [](https://pypi.org/project/mailtrap/) | ||
| [](https://pypi.org/project/mailtrap/) | ||
|
|
||
|
|
@@ -27,34 +27,86 @@ pip install mailtrap | |
|
|
||
| ## Usage | ||
|
|
||
| ### Minimal | ||
| ### Minimal usage (Transactional sending) | ||
|
|
||
| ```python | ||
| import mailtrap as mt | ||
|
|
||
| # create mail object | ||
| API_TOKEN = "<YOUR_API_TOKEN>" # your API key here https://mailtrap.io/api-tokens | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN) | ||
|
|
||
| # Create mail object | ||
| mail = mt.Mail( | ||
| sender=mt.Address(email="[email protected]", name="Mailtrap Test"), | ||
| to=[mt.Address(email="[email protected]")], | ||
| sender=mt.Address(email="[email protected]", name="John Smith"), | ||
| to=[mt.Address(email="[email protected]")], | ||
| subject="You are awesome!", | ||
| text="Congrats for sending test email with Mailtrap!", | ||
| ) | ||
|
|
||
| client.send(mail) | ||
| ``` | ||
|
|
||
| ### Sandbox vs Production (easy switching) | ||
|
|
||
| Mailtrap lets you test safely in the Email Sandbox and then switch to Production (Sending). | ||
| Remove the inbox_id field or set it to None. Then, remove the sandbox field or set it to False. | ||
| You can change the arguments in the code or via another way. Here is an example using environment variables. | ||
|
|
||
| Set next environment variables: | ||
| ```bash | ||
| MAILTRAP_API_KEY=your_api_token # https://mailtrap.io/api-tokens | ||
| MAILTRAP_USE_SANDBOX=true # true/false toggle | ||
| MAILTRAP_INBOX_ID=123456 # Only needed for sandbox | ||
| ``` | ||
|
|
||
| Bootstrap logic: | ||
| ```python | ||
| import os | ||
| import mailtrap as mt | ||
|
|
||
| API_KEY = os.environ["MAILTRAP_API_KEY"] | ||
| IS_SANDBOX = os.environ.get("MAILTRAP_USE_SANDBOX", "true").lower() == "true" | ||
| INBOX_ID = os.environ.get("MAILTRAP_INBOX_ID") | ||
|
|
||
| client = mt.MailtrapClient( | ||
| token=API_KEY, | ||
| sandbox=IS_SANDBOX, | ||
| inbox_id=INBOX_ID, # None is ignored for production | ||
| ) | ||
|
|
||
| # Create mail object | ||
| mail = mt.Mail( | ||
| sender=mt.Address(email="[email protected]", name="John Smith"), | ||
| 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 | ||
| Bulk stream example (optional) differs only by setting `bulk=True`: | ||
| `bulk_client = mt.MailtrapClient(token=API_KEY, bulk=True)` | ||
|
|
||
| Recommendations: | ||
| - Use separate API tokens for Production and Sandbox. | ||
| - Keep initialisation in a single factory object/service so that switching is centralised. | ||
|
|
||
| ### Full-featured usage example | ||
|
|
||
| ```python | ||
| import base64 | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| import mailtrap as mt | ||
|
|
||
| client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"]) | ||
|
|
||
| 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")], | ||
|
|
@@ -100,15 +152,17 @@ mail = mt.Mail( | |
| custom_variables={"year": 2023}, | ||
| ) | ||
|
|
||
| client = mt.MailtrapClient(token="your-api-key") | ||
| client.send(mail) | ||
| ``` | ||
|
|
||
| ### Using email template | ||
| ### Minimal usage of email template | ||
|
|
||
| ```python | ||
| import os | ||
| import mailtrap as mt | ||
|
|
||
| client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"]) | ||
|
|
||
| # create mail object | ||
| mail = mt.MailFromTemplate( | ||
| sender=mt.Address(email="[email protected]", name="Mailtrap Test"), | ||
|
|
@@ -117,21 +171,137 @@ mail = mt.MailFromTemplate( | |
| template_variables={"user_name": "John Doe"}, | ||
| ) | ||
|
|
||
| # create client and send | ||
| client = mt.MailtrapClient(token="your-api-key") | ||
| client.send(mail) | ||
| ``` | ||
|
|
||
| ### Sending email directly via SendingApi | ||
|
|
||
| This approach is newer. It can be useful when you expect the response to be model-based rather than dictionary-based, as in MailtrapClient.send(). | ||
|
|
||
| ```python | ||
| import os | ||
| import mailtrap as mt | ||
|
|
||
| client = mt.MailtrapClient(token=os.environ["MAILTRAP_API_KEY"]) | ||
| sending_api = client.sending_api | ||
|
|
||
| # create mail object | ||
| mail = mt.Mail( | ||
| sender=mt.Address(email="[email protected]", name="John Smith"), | ||
| to=[mt.Address(email="[email protected]")], | ||
| subject="You are awesome!", | ||
| text="Congrats for sending test email with Mailtrap!", | ||
| ) | ||
|
|
||
| sending_api.send(mail) | ||
| ``` | ||
| #### Mailtrap sending responses difference | ||
|
|
||
| #### 1. `client.send()` | ||
| **Response:** | ||
| ```python | ||
| { | ||
| "success": True, | ||
| "message_ids": ["5162954175"] | ||
| } | ||
| ``` | ||
|
|
||
| #### 2. `client.sending_api.send()` | ||
| **Response:** | ||
| ```python | ||
| SendingMailResponse(success=True, message_ids=["5162955057"]) | ||
| ``` | ||
|
|
||
| The same situation applies to both `client.batch_send()` and `client.sending_api.batch_send()`. | ||
|
|
||
| ### All usage examples | ||
|
|
||
| Refer to the [examples](examples) folder for the source code of this and other advanced examples. | ||
|
|
||
| ### Sending API | ||
| - [Sending minimal](examples/sending/minimal_sending.py) | ||
| - [Sending advanced](examples/sending/advanced_sending.py) | ||
| - [Sending using template](examples/sending/sending_with_template.py) | ||
|
|
||
| ### Batch Sending API | ||
| - [Batch sending minimal](examples/sending/batch_minimal_sending.py) | ||
| - [Batch sending advanced](examples/sending/batch_advanced_sending.py) | ||
| - [Batch sending using template](examples/sending/batch_sending_with_template.py) | ||
|
|
||
| ### Sandbox (Email Testing) API | ||
| - [Attachments](examples/testing/attachments.py) | ||
| - [Inboxes](examples/testing/inboxes.py) | ||
| - [Messages](examples/testing/messages.py) | ||
| - [Projects](examples/testing/projects.py) | ||
|
|
||
| ### Contacts API | ||
| - [Contacts](examples/contacts/contacts.py) | ||
| - [Contact Events](examples/contacts/contact_events.py) | ||
| - [Contact Exports](examples/contacts/contact_exports.py) | ||
| - [Contact Fields](examples/contacts/contact_fields.py) | ||
| - [Contact Imports](examples/contacts/contact_imports.py) | ||
| - [Contact Lists](examples/contacts/contact_lists.py) | ||
|
|
||
| ### Email Templates API | ||
| - [Email Templates](examples/email_templates/templates.py) | ||
|
|
||
| ### Suppressions API | ||
| - [Suppressions](examples/suppressions/suppressions.py) | ||
|
|
||
| ### General API | ||
| - [Account Accesses](examples/general/account_accesses.py) | ||
| - [Accounts](examples/general/accounts.py) | ||
| - [Billing](examples/general/billing.py) | ||
| - [Permissions](examples/general/permissions.py) | ||
|
|
||
| ## Supported functionality | ||
|
|
||
| This Python package offers integration with the [official API](https://api-docs.mailtrap.io/) for [Mailtrap](https://mailtrap.io). | ||
|
|
||
| Quickly integrate Mailtrap with your Python app. | ||
|
|
||
| Currently, with this SDK you can: | ||
| - Email API/SMTP | ||
| - Send an email (Transactional and Bulk streams) | ||
| - Send an email with a template (Transactional and Bulk streams) | ||
| - Send a batch of emails (Transactional and Bulk streams) | ||
| - Send a batch of emails with a template (Transactional and Bulk streams) | ||
| - Email Sandbox (Testing) | ||
| - Send an email | ||
| - Send an email with a template | ||
| - Send a batch of emails | ||
| - Send a batch of emails with a template | ||
| - Messages management | ||
| - Inboxes management | ||
| - Projects management | ||
| - Attachments management | ||
| - Contacts | ||
| - Contacts management | ||
| - Contact Lists management | ||
| - Contact Fields management | ||
| - Contact Events management | ||
| - Contact Exports management | ||
| - Contact Imports management | ||
| - Suppressions | ||
| - Suppressions management (find and delete) | ||
| - Templates | ||
| - Templates management | ||
| - General | ||
| - Account access management | ||
| - Permissions management | ||
| - List accounts you have access to | ||
| - Get current billing information | ||
|
|
||
| ## 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). | ||
| Bug reports and pull requests are welcome on [GitHub](https://github.com/mailtrap/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 | ||
| git clone https://github.com/mailtrap/mailtrap-python.git | ||
| cd mailtrap-python | ||
| ``` | ||
|
|
||
|
|
||
This file was deleted.
Oops, something went wrong.
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.