SDKs overview
Mandrill, a transactional email API service, provides various Software Development Kits (SDKs) and client libraries to simplify integration for developers. These libraries abstract the underlying HTTP API requests, allowing developers to interact with Mandrill's services using native language constructs rather than direct API calls. The official SDKs support common programming languages, with community-maintained libraries expanding the ecosystem.
The primary function of these SDKs is to enable programmatic sending of transactional emails, management of email templates, and access to email analytics. Developers can utilize these tools to automate email workflows within their applications. All interactions with Mandrill's API, whether via SDKs or direct HTTP requests, require an API key for authentication, as detailed in the Mailchimp Transactional API Getting Started guide.
Official SDKs by language
Mandrill offers official client libraries for several popular programming languages, maintained by Mailchimp. These libraries are generally the recommended approach for integrating with the Mandrill API due to direct support from the vendor and consistent updates.
| Language | Package/Repository | Maturity | Installation Command Example |
|---|---|---|---|
| PHP | mandrill/mandrill |
Stable | composer require mandrill/mandrill |
| Ruby | mandrill-api |
Stable | gem install mandrill-api |
| Python | mandrill |
Stable | pip install mandrill |
| Node.js | mandrill-api |
Stable | npm install mandrill-api |
| Go | github.com/mailchimp/mandrill-go |
Stable | go get github.com/mailchimp/mandrill-go |
| C# | Mailchimp.Mandrill.net (NuGet) |
Stable | Install-Package Mailchimp.Mandrill.net |
| Java | com.mailchimp/mandrill-api (Maven) |
Stable | (See Maven/Gradle config below) |
Installation
Installation methods vary by programming language and package manager. Below are typical installation instructions for the officially supported SDKs.
PHP
composer require mandrill/mandrill
After installation, you can initialize the client:
require_once 'vendor/autoload.php';
$mandrill = new Mandrill('YOUR_API_KEY');
Ruby
gem install mandrill-api
Initialize the client in your Ruby application:
require 'mandrill' # Gem is mandrill-api, require is mandrill
mandrill = Mandrill::API.new 'YOUR_API_KEY'
Python
pip install mandrill
Initialize the client:
import mandrill
mandrill_client = mandrill.Mandrill('YOUR_API_KEY')
Node.js
npm install mandrill-api
Initialize the client in JavaScript:
var mandrill = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill('YOUR_API_KEY');
Go
go get github.com/mailchimp/mandrill-go
You would typically import and use the library similar to other Go packages, setting the API key:
import (
"github.com/mailchimp/mandrill-go"
)
func main() {
apiKey := "YOUR_API_KEY"
client := mandrill.NewClient(apiKey)
// ... use client ...
}
C#
Using NuGet Package Manager:
Install-Package Mailchimp.Mandrill.net
Or via .NET CLI:
dotnet add package Mailchimp.Mandrill.net
Initialize the client in C#:
using Mandrill.net;
var mandrillClient = new MandrillClient("YOUR_API_KEY");
Java
For Maven, add the following to your pom.xml:
<dependency>
<groupId>com.mailchimp</groupId>
<artifactId>mandrill-api</artifactId>
<version>1.0.5</version> <!-- Check for the latest version -->
</dependency>
For Gradle, add to your build.gradle:
implementation 'com.mailchimp:mandrill-api:1.0.5' <!-- Check for the latest version -->
Initialize the client in Java:
import com.mandrill.MandrillApi;
import com.mandrill.model.request.MandrillMessage;
MandrillApi mandrillApi = new MandrillApi("YOUR_API_KEY");
Quickstart example
This example demonstrates sending a simple transactional email using the Python SDK. Similar logic applies across other languages, adapting to their native syntax and object models.
Python quickstart
import mandrill
try:
mandrill_client = mandrill.Mandrill('YOUR_API_KEY')
message = {
'html': '<p>Example HTML content.</p>',
'text': 'Example text content.',
'subject': 'Your Mandrill Test Email',
'from_email': '[email protected]',
'from_name': 'Example Sender',
'to': [
{
'email': '[email protected]',
'name': 'Recipient Name',
'type': 'to'
}
],
'headers': {'Reply-To': '[email protected]'},
'important': False,
'track_opens': True,
'track_clicks': True,
'auto_text': True,
'auto_html': False,
'inline_css': False,
'url_strip_qs': False,
'preserve_recipients': True,
'view_content_link': False,
'bcc_address': '[email protected]',
'merge': True,
'global_merge_vars': [
{'name': 'merge1', 'content': 'merge1 content'}
],
'merge_vars': [
{
'rcpt': '[email protected]',
'vars': [
{'name': 'merge2', 'content': 'merge2 content'}
]
}
],
'tags': ['password-resets'],
'subaccount': 'my_subaccount_id', # Optional: if using subaccounts
'google_analytics_domains': ['example.com'],
'google_analytics_campaign': 'mandrill_quickstart',
'metadata': {'website': 'www.example.com'},
'recipient_metadata': [
{
'rcpt': '[email protected]',
'values': {'user_id': 123456}
}
],
'attachments': [
{
'type': 'text/plain',
'name': 'myfile.txt',
'content': 'ZXhhbXBsZSBmaWxlIGNvbnRlbnQ='
}
]
}
result = mandrill_client.messages.send(message=message, async=False, ip_pool='Main Pool', send_at=None)
print(result)
except mandrill.Error as e:
# Mandrill errors are thrown as exceptions
print(f'A mandrill error occurred: {e.__class__} - {e}')
raise
This Python code initializes the Mandrill client with an API key, constructs a detailed message dictionary, and sends the email. The message dictionary can include various parameters such as HTML/text content, subject, sender/recipient details, and optional features like tracking, tags, and attachments. For a comprehensive list of message parameters, refer to the Mandrill Messages Send API documentation.
Community libraries
Beyond the official SDKs, the developer community has contributed various libraries and wrappers for Mandrill in languages not officially supported or to provide alternative interfaces. While these can be valuable, their maintenance status and feature parity with the official API may vary. Developers are encouraged to evaluate community libraries based on their specific needs, looking at factors like recent updates, community support, and alignment with the latest Mandrill API versions.
Examples of languages with community-contributed Mandrill libraries can include:
- Elixir: Libraries like
mandrill-ex - Rust: Wrappers for the Mandrill API
- Swift/Objective-C: Libraries for iOS/macOS development
- Dart/Flutter: Packages for mobile and web applications
When considering a community library, it is advisable to check its repository for activity, documentation, and open issues. For instance, platforms like GitHub host many such projects, and their README files often contain installation and usage instructions. The Mozilla Developer Network's API key guide provides general best practices for handling API keys, relevant for both official and community libraries.
It is important to note that Mandrill is now exclusively an add-on for paid Mailchimp accounts. This integration model means that developers using Mandrill will also be operating within the broader Mailchimp ecosystem, potentially influencing the choice and maintenance of SDKs and libraries as noted in their transactional email developer guide.