SDKs overview
Mailtrap offers a range of Software Development Kits (SDKs) and client libraries designed to facilitate interaction with its email testing and sending platforms. These tools enable developers to integrate Mailtrap's functionalities directly into their applications, abstracting the complexities of HTTP requests and API authentication. The SDKs cover popular programming languages, providing idiomatic interfaces for tasks such as sending transactional emails, managing email testing inboxes, and retrieving email data for quality assurance.
The primary benefit of using an SDK over direct API calls is the reduction in boilerplate code and improved developer experience. SDKs often include built-in request builders, response parsers, and error handling mechanisms, allowing developers to focus on application logic rather than low-level API communication. Mailtrap's official SDKs are maintained by the Mailtrap team, while a broader ecosystem of community-contributed libraries also exists, offering additional language support or specialized features. For a comprehensive overview of all available API endpoints, developers can consult the Mailtrap API Reference documentation.
Official SDKs by language
Mailtrap provides official SDKs for several programming languages, covering both its Email Testing (SMTP Sandbox) and Email Sending (Email API/SMTP Service) functionalities. These libraries are designed to offer a consistent and reliable way to interact with Mailtrap's services. The following table outlines the key official SDKs:
| Language | Package Name | Installation Command | Maturity | Documentation Link |
|---|---|---|---|---|
| Ruby | mailtrap |
gem install mailtrap |
Stable | Ruby SDK Docs |
| PHP | mailtrap/mailtrap-php |
composer require mailtrap/mailtrap-php |
Stable | PHP SDK Docs |
| Python | mailtrap-python |
pip install mailtrap-python |
Stable | Python SDK Docs |
| Node.js | mailtrap |
npm install mailtrap |
Stable | Node.js SDK Docs |
| Go | github.com/mailtrap/mailtrap-go |
go get github.com/mailtrap/mailtrap-go |
Stable | Go SDK Docs |
| Java | mailtrap-java |
(Maven/Gradle dependency) | Stable | Java SDK Docs |
| C# | Mailtrap.Net |
dotnet add package Mailtrap.Net |
Stable | C# SDK Docs |
Installation
Installing a Mailtrap SDK typically involves using the package manager specific to your programming language. The following provides general installation instructions for the officially supported languages. For detailed, language-specific prerequisites and advanced configuration, always refer to the official Mailtrap documentation.
Ruby
To add the Mailtrap Ruby gem to your project:
gem install mailtrap
If you're using Bundler, add this line to your Gemfile:
gem 'mailtrap'
Then run bundle install.
PHP
Install the Mailtrap PHP SDK via Composer:
composer require mailtrap/mailtrap-php
Python
Install the Mailtrap Python library using pip:
pip install mailtrap-python
Node.js
Install the Mailtrap Node.js package via npm:
npm install mailtrap
Or using Yarn:
yarn add mailtrap
Go
Get the Mailtrap Go module:
go get github.com/mailtrap/mailtrap-go
Java
For Java projects, you typically include the Mailtrap SDK as a dependency in your build tool (e.g., Maven or Gradle). The specific snippet will be available in the Java SDK documentation. For Maven, it might look like this in your pom.xml:
<dependency>
<groupId>io.mailtrap</groupId>
<artifactId>mailtrap-java</artifactId>
<version>1.0.0</version> <!-- Check for the latest version -->
</dependency>
C#
Install the Mailtrap .NET SDK using the .NET CLI:
dotnet add package Mailtrap.Net
Alternatively, you can install it via the NuGet Package Manager in Visual Studio.
Quickstart example
This quickstart example demonstrates how to send an email using the Mailtrap Email Sending API with the Python SDK. This assumes you have already obtained your Mailtrap API token for Sending and have installed the Python SDK as described above. The Mailtrap Email Sending API is designed for reliable delivery of transactional and bulk emails, as detailed in the Mailtrap Email Sending API documentation.
from mailtrap import MailtrapClient
from mailtrap.models import SendEmailRequest
# Replace with your actual Mailtrap API Token for Sending
MAILTRAP_API_TOKEN = "YOUR_MAILTRAP_SENDING_API_TOKEN"
client = MailtrapClient(token=MAILTRAP_API_TOKEN)
# Build the email request
email_request = SendEmailRequest(
from_email={"email": "[email protected]", "name": "Mailtrap Test"},
to=[{"email": "[email protected]"}],
subject="Hello from Mailtrap Python SDK!",
text="This is a test email sent using the Mailtrap Python SDK.",
category="Testing"
)
try:
response = client.send(email_request)
print("Email sent successfully!")
print(f"Message ID: {response.message_ids[0]}")
print(f"Response: {response.pretty_print()}")
except Exception as e:
print(f"Failed to send email: {e}")
This example initializes the Mailtrap client with an API token, constructs an email payload including sender, recipient, subject, and body, and then attempts to send the email. The response will contain information about the sent message, including a message ID. For testing emails without actually sending them to external recipients, developers can configure their application to send to a Mailtrap Email Testing inbox via SMTP, as outlined in Mailtrap's SMTP setup guide.
Community libraries
Beyond the official SDKs, the Mailtrap ecosystem benefits from community-driven libraries and integrations. These libraries can offer support for additional programming languages, frameworks, or specialized use cases not directly covered by the official offerings. While official SDKs are maintained directly by Mailtrap, community libraries are developed and supported by individual developers or groups within the broader open-source community. Developers considering a community library should verify its active maintenance, documentation, and compatibility with the latest Mailtrap API versions.
Examples of common integrations often include adapters for popular web frameworks or ORMs, allowing seamless email sending within the framework's existing conventions. For instance, a community library might provide a wrapper for sending emails through Mailtrap within a Django application, or a plugin for a content management system. These extensions demonstrate the versatility of Mailtrap's API and the ability of developers to build upon its core services. When evaluating community solutions, reviewing the project's GitHub repository for recent commits, open issues, and pull requests can provide insight into its current status and reliability. Resources like MDN Web Docs on HTTP Status Codes can be useful for understanding API responses from any client library, official or community-driven.
For a comprehensive list of community-maintained integrations or examples of how to interact with the Mailtrap API in other languages, developers are encouraged to explore community forums, GitHub repositories, and general programming resources. The official Mailtrap documentation also sometimes links to popular community contributions where applicable, offering a curated starting point for broader integration needs.