SDKs overview

mail.gw provides Software Development Kits (SDKs) and client libraries designed to simplify integration with its email API. These SDKs wrap the underlying RESTful API, offering language-specific methods for common operations such as sending emails, managing mailing lists, validating email addresses, and handling webhooks. By using an SDK, developers can interact with mail.gw services without directly constructing HTTP requests or parsing JSON responses, which can reduce development time and potential error rates. The official SDKs are maintained by mail.gw, while a broader ecosystem of community-contributed libraries also exists, often offering specialized features or support for additional programming languages.

The mail.gw API itself is a comprehensive interface for email management, supporting various functionalities from simple email sending to advanced routing and analytics. It adheres to standard web protocols, primarily HTTP/S for communication and JSON for data interchange. Developers can consult the detailed mail.gw API reference documentation for direct API interaction, or opt for the convenience of an SDK for a more idiomatic experience within their chosen programming environment.

Official SDKs by language

mail.gw maintains official SDKs for several popular programming languages, ensuring direct support and compatibility with the latest API features. These libraries are typically distributed through standard package managers for each language, facilitating easy inclusion in development projects. Using an official SDK can provide benefits such as consistent API schema adherence, built-in error handling, and direct access to mail.gw support resources.

The following table outlines the official SDKs, their respective package names, installation commands, and general maturity status:

Language Package Name Installation Command Maturity
Python mailgun-sdk pip install mailgun-sdk Stable, actively maintained
Ruby mailgun-ruby gem install mailgun-ruby Stable, actively maintained
Java mailgun-java Add to pom.xml (Maven) or build.gradle (Gradle) Stable, actively maintained
PHP mailgun/mailgun-php composer require mailgun/mailgun-php Stable, actively maintained
Go github.com/mailgun/mailgun-go go get github.com/mailgun/mailgun-go/v4 Stable, actively maintained
Node.js mailgun.js npm install mailgun.js or yarn add mailgun.js Stable, actively maintained

Each SDK is designed to align with the conventions and best practices of its respective language ecosystem, offering developers an intuitive way to interact with mail.gw's services. For example, the Python SDK might use object-oriented structures familiar to Python developers, while the Node.js library would typically support asynchronous operations using Promises or async/await syntax.

Installation

Installing mail.gw SDKs typically follows the standard procedures for each programming language's package management system. Below are general installation instructions for the officially supported SDKs.

Python

The Python SDK can be installed using pip, the Python package installer. It is recommended to use a Python virtual environment to manage dependencies.

pip install mailgun-sdk

After installation, you can import the library and begin using it in your Python applications.

Ruby

The Ruby SDK is available as a gem and can be installed using the gem command:

gem install mailgun-ruby

Once installed, require the gem in your Ruby project to access its functionalities.

Java

For Java projects, the mail.gw SDK can be integrated using build automation tools like Maven or Gradle. You'll need to add the appropriate dependency to your project's configuration file.

Maven (pom.xml):

<dependency>
    <groupId>com.mailgun</groupId>
    <artifactId>mailgun-java</artifactId>
    <version>YOUR_MAILGUN_JAVA_VERSION</version>
</dependency>

Gradle (build.gradle):

implementation 'com.mailgun:mailgun-java:YOUR_MAILGUN_JAVA_VERSION'

Replace YOUR_MAILGUN_JAVA_VERSION with the latest version available in the mail.gw documentation or a Maven repository.

PHP

The PHP SDK is managed via Composer, the PHP dependency manager:

composer require mailgun/mailgun-php

After running this command, Composer will add the necessary files to your vendor/ directory, and you can include the autoloader in your PHP scripts.

Go

For Go applications, the SDK is fetched using the go get command:

go get github.com/mailgun/mailgun-go/v4

Ensure you are using Go modules in your project for proper dependency management. The v4 in the path indicates the major version, which is important for Go's module system.

Node.js

The Node.js SDK can be installed using npm or Yarn:

npm:

npm install mailgun.js

Yarn:

yarn add mailgun.js

Once installed, you can import the library into your JavaScript or TypeScript files, typically using ES modules or CommonJS syntax.

Quickstart example

This quickstart example demonstrates how to send a simple email using the mail.gw Python SDK. Before running, ensure you have set up your mail.gw API key and domain.

import os
from mailgun.mailgun import Mailgun

# Replace with your actual Mailgun API key and domain
API_KEY = os.environ.get('MAILGUN_API_KEY') # It's recommended to use environment variables
DOMAIN = os.environ.get('MAILGUN_DOMAIN')

if not API_KEY or not DOMAIN:
    print("Please set MAILGUN_API_KEY and MAILGUN_DOMAIN environment variables.")
    exit()

mgun = Mailgun(api_key=API_KEY, domain=DOMAIN)

def send_simple_message():
    try:
        result = mgun.send_message(
            from_email=f"Excited User <mailgun@{DOMAIN}>",
            to_email="[email protected]", # Replace with recipient email
            subject="Hello from Mailgun!",
            text="Congratulations, you just sent an email with Mailgun!"
        )
        print(f"Email sent successfully! Message ID: {result['id']}")
    except Exception as e:
        print(f"Error sending email: {e}")

if __name__ == '__main__':
    send_simple_message()

To execute this code:

  1. Install the Python SDK: pip install mailgun-sdk
  2. Set your environment variables: export MAILGUN_API_KEY='YOUR_API_KEY' and export MAILGUN_DOMAIN='YOUR_MAILGUN_DOMAIN'. Your API key can be found in your mail.gw control panel.
  3. Replace "[email protected]" with the actual email address you wish to send to.
  4. Run the script: python your_script_name.py

This example demonstrates the basic steps for initializing the client and calling the send_message method. The SDK handles the underlying HTTP POST request to the mail.gw API endpoint, including authentication and parameter serialization. For more advanced features like sending HTML emails, attachments, or using templates, refer to the mail.gw Python SDK documentation.

Community libraries

Beyond the official offerings, the mail.gw ecosystem benefits from a variety of community-developed libraries and wrappers. These libraries often extend functionality, provide alternative interfaces, or support languages not officially covered by mail.gw. While community libraries can offer flexibility and innovation, it's important to consider their maintenance status, documentation quality, and compatibility with the latest mail.gw API versions.

Developers often contribute to community projects to address specific use cases, integrate with particular frameworks, or simply offer a different stylistic approach to API interaction. For instance, a community library might provide a wrapper optimized for a specific web framework like Django or Ruby on Rails, or offer advanced features such as automatic retry mechanisms or enhanced logging capabilities. Resources like GitHub and language-specific package repositories (e.g., PyPI for Python, RubyGems for Ruby, npm for Node.js) are good places to discover these contributions.

When choosing a community library, it is advisable to check the project's activity, recent commit history, open issues, and pull requests to gauge its ongoing support. It's also prudent to review the source code for security best practices and ensure it aligns with your project's technical requirements. While mail.gw's official documentation focuses on its own SDKs, general guides on API authentication methods like basic authentication or bearer tokens can be useful for evaluating third-party wrappers, as these often implement similar underlying mechanisms for communicating with the mail.gw API.

Community libraries can be particularly useful for niche integrations or when specific performance characteristics are required that are not directly addressed by the official SDKs. However, for critical production systems, the stability and support of official SDKs often make them the preferred choice.