Mailgun Documentation

Last updated on
30 April 2025

Installation

The module requires an official PHP-SDK. You may verify that the Mailgun library has been properly installed by going to http://yoursite.com/admin/reports/status

Drupal 7

There are three ways to install the SDK:

1) The preferred and most simple way is:
use Composer Manager contrib module and follow instructions from it.

2) If you're using Libraries API contrib module:

cd webroot/sites/all/libraries
mkdir mailgun
cd mailgun      
composer require "mailgun/mailgun-php:~2.0" "php-http/guzzle6-adapter:~1.0"

You may need to clear cache once this is done.

3) If you are using a composer-based project (i.e. https://github.com/drupal-composer/drupal-project) then no additional actions required. All you need is composer require drupal/mailgun and it'll take care of all need dependencies.

Please make sure you have autoloader in place.

Actually, all recommended ways require Composer because Mailgun requires other dependencies.

Drupal 8

We highly recommend using Composer to manage your Drupal dependencies. See "Using Composer with Drupal".
In this case, all you need is:

composer require drupal/mailgun

All needed dependencies will be installed automatically.

Probably, you can install Drupal module in another way.
In this case, you should take care of Mailgun dependencies by yourself.

You can read more about Mailgun dependencies here.


Usage

Mailgun leverages the Mail System module to provide a flexible way of managing which e-mails are sent by which module. Visit admin/config/system/mailsystem to configure. For example, if you wish to send all system e-mails using Mailgun, you would select MailgunMailSystem from the drop-down menu of Site-wide default MailSystemInterface class.

If you have not added the Mailgun-required DNS records and you want to test the module, then email fired only your configure mailgun email account so other email id is not working I think this is trying to say you can only use the mailgun test account and any non-test mail sent will not work -- but I am not sure. Either way, you need to configure your Mailgun account properly, and be careful about your Mailgun reputation!


HTML Support

There is no need for installing the Mime Mail or HTML Mail modules, since Mailgun itself provides MIME support at the mail server level. See Mailgun documentation for details.


API

This module provides a mailgun_get_client() function, which returns the Mailgun client object allowing you to utilize Mailgun's other API features. See mailgun.module for more information.


Examples

Suppose you want to get a list of all the domains associated with the API key, you would:

$client = mailgun_get_client();
// You could also supply your own API key like this:
// $client = mailgun_get_client('key-xxxxxxxx');

try {
  // Use the $client variable to perform any action you want. Here we're just going to get a list of domains.
  $result = $client->get('domains');
  // For a list of all the response codes, see: https://documentation.mailgun.com/api-intro.html#errors
  switch ($result->http_response_code) {
    case 200:
      print 'Everything worked as expected.';
      break;
    case 400:
      print 'Bad Request - Often missing a required parameter.';
      break;
    case 401:
      print 'Unauthorized - No valid API key provided.';
      break;
    case 402:
      print 'Request Failed - Parameters were valid but request failed.';
      break;
    case 404:
      print 'Not Found - The requested item doesn’t exist.';
      break;
    case 500:
    case 502:
    case 503:
    case 504:
      print 'Server Errors - something is wrong on Mailgun’s end.';
      break;
  }
  print "\n";
  print $result->http_response_body->total_count . " items retrieved.\n";
  foreach ($result->http_response_body->items as $item) {
    print 'Domain: ' . $item->name . "\n";
    print 'Created at: ' . $item->created_at . "\n";
    print 'State: ' . $item->state . "\n";
    print 'Type: ' . $item->type . "\n\n";
  }
} catch (Exception $e) {
  print 'Exception occurred: ' . $e->getCode() . ': ' . $e->getMessage();
}

Help improve this page

Page status: Not set

You can: