SDKs overview
Mailgun provides Software Development Kits (SDKs) to facilitate interaction with its email API. These SDKs abstract the underlying HTTP requests and JSON responses, presenting a more idiomatic interface for developers working in specific programming languages. The primary benefit of using an SDK is to reduce boilerplate code and potential errors associated with direct API calls, such as request formatting, authentication, and error handling. Mailgun's SDKs are designed to streamline common operations, including sending emails, managing mailing lists, validating email addresses, and configuring webhooks for event notifications. This approach helps developers integrate email capabilities into their applications more rapidly and with fewer development cycles, as detailed in the Mailgun developer documentation.
The SDKs typically handle API authentication using API keys, which are passed during client initialization. They also provide methods for constructing email messages with various parameters, such as recipients, subject lines, HTML bodies, text bodies, attachments, and custom headers. For inbound email processing, the SDKs can assist in parsing incoming email data received via webhooks. By offering these language-specific tools, Mailgun aims to lower the barrier to entry for developers and promote efficient use of its platform across a diverse range of application environments.
Official SDKs by language
Mailgun maintains official SDKs for several popular programming languages, ensuring direct support and compatibility with the latest API features. These SDKs are developed and maintained by Mailgun, offering a reliable and recommended method for integrating with the service. Each SDK is tailored to the conventions and best practices of its respective language, providing a native development experience.
| Language | Package Name | Install Command | Maturity |
|---|---|---|---|
| Python | mailgun-sdk |
pip install mailgun-sdk |
Production Ready |
| Ruby | mailgun-ruby |
gem install mailgun-ruby |
Production Ready |
| PHP | mailgun/mailgun-php |
composer require mailgun/mailgun-php |
Production Ready |
| Java | com.mailgun:mailgun-java |
Maven: Add to pom.xml dependenciesGradle: Add to build.gradle dependencies |
Production Ready |
| C# | Mailgun.NET |
Install-Package Mailgun.NET |
Production Ready |
| Go | github.com/mailgun/mailgun-go |
go get github.com/mailgun/mailgun-go/v4 |
Production Ready |
| Node.js | mailgun.js |
npm install mailgun.js |
Production Ready |
Installation
Installing a Mailgun SDK typically involves using the standard package manager for the chosen programming language. The process is designed to be straightforward, allowing developers to quickly add the necessary libraries to their project dependencies. Below are detailed installation instructions for each official SDK, ensuring that developers can get started efficiently.
Python
The Python SDK can be installed using pip, the Python package installer. It is recommended to use a virtual environment to manage project dependencies.
pip install mailgun-sdk
Ruby
For Ruby projects, the SDK is available as a gem and can be installed using Bundler or directly with gem install.
gem install mailgun-ruby
PHP
PHP projects commonly use Composer for dependency management. The Mailgun PHP SDK is available via Packagist.
composer require mailgun/mailgun-php
Java
Java developers can integrate the Mailgun SDK using Maven or Gradle. The dependency information needs to be added to the project's build file.
Maven
<dependency>
<groupId>com.mailgun</groupId>
<artifactId>mailgun-java</artifactId&n>
<version>[latest-version]</version>
</dependency>
Gradle
implementation 'com.mailgun:mailgun-java:[latest-version]'
Replace [latest-version] with the current stable version number, which can be found on the Mailgun Java SDK documentation.
C#
The C# SDK is distributed as a NuGet package, which can be installed via the NuGet Package Manager Console or the .NET CLI.
Install-Package Mailgun.NET
Or using the .NET CLI:
dotnet add package Mailgun.NET
Go
Go modules are used to manage dependencies for the Mailgun Go SDK. The go get command fetches the module.
go get github.com/mailgun/mailgun-go/v4
Node.js
The Node.js SDK is available on npm and can be installed using npm or yarn.
npm install mailgun.js
Or with Yarn:
yarn add mailgun.js
Quickstart example
This quickstart example demonstrates sending a simple email using the Mailgun Node.js SDK. This particular language is chosen for its widespread use in web development and the clarity of its syntax for API interactions. The example covers initialization, email construction, and sending. Before running this code, ensure you have your Mailgun API key and domain configured, which are essential for authentication and sending emails through the Mailgun service.
First, ensure you have mailgun.js installed as per the installation instructions above.
const formData = require('form-data');
const Mailgun = require('mailgun.js');
const mailgun = new Mailgun(formData);
const client = mailgun.client({
username: 'api',
key: process.env.MAILGUN_API_KEY || 'YOUR_MAILGUN_API_KEY'
});
const DOMAIN = process.env.MAILGUN_DOMAIN || 'YOUR_MAILGUN_DOMAIN';
client.messages.create(DOMAIN, {
from: "Excited User <mailgun@YOUR_MAILGUN_DOMAIN>",
to: ["[email protected]", "[email protected]"],
subject: "Hello from Mailgun!",
text: "Testing some Mailgun awesomeness!",
html: "<h1>Testing some Mailgun awesomeness!</h1>"
})
.then(msg => console.log(msg)) // logs response data
.catch(err => console.error(err)); // logs any error
In this example:
formDatais imported and used to initialize the Mailgun client, which is necessary for handling multipart form data for requests.- The
clientis instantiated with your Mailgun API key. It's best practice to load this from environment variables (process.env.MAILGUN_API_KEY) rather than hardcoding it. DOMAINis set to your Mailgun sending domain, also ideally from an environment variable. This domain must be configured and verified within your Mailgun account.- The
client.messages.create()method is called with the sending domain and an object containing email parameters:from,to,subject,text, andhtml. - The
.then()block handles a successful response, typically logging the message ID and other details returned by the Mailgun API. - The
.catch()block handles any errors that occur during the API call, providing error details for debugging.
For more detailed examples and advanced features like attachments, templates, and custom data, refer to the Mailgun API Reference and the specific SDK documentation for your chosen language.
Community libraries
While Mailgun provides official SDKs, the developer community also contributes various libraries and wrappers that extend functionality or offer alternative implementations. These community-driven projects can sometimes provide support for less common languages, specific frameworks, or unique use cases not covered by the official SDKs. Developers often create these libraries to integrate Mailgun into their preferred technology stacks or to add features like advanced webhook processing, local development tools, or specific ORM integrations.
When considering a community library, it is important to evaluate its maintenance status, community support, and compatibility with the latest Mailgun API versions. Factors such as the number of contributors, recent commit history, and issue resolution rate on platforms like GitHub can indicate the library's reliability. While official SDKs are generally recommended for stability and direct support, community libraries can offer flexibility and innovation. For example, developers building applications on frameworks like Laravel or Django might find community-maintained packages that offer more seamless integration with those ecosystems than a generic SDK. Another example of community-driven development in the API ecosystem is the Kong API Gateway, which also benefits from a broad community of plugin developers, demonstrating the value of open contributions to API tooling.
Developers should consult the Mailgun documentation or community forums for recommendations and discussions regarding third-party libraries. Always verify the security implications and licensing of any community-contributed code before integrating it into a production environment.