Installation and Usage - backdrop-contrib/mailgun GitHub Wiki

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

Installing the SDK

You can use Composer Manager to help, or if you are already managing libraries separately with Composer that will work, too. As long as the \Mailgun\Mailgun class can autoload when needed, it will be detected correctly.

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.

Your Mailgun account may also need configuration related to DNS settings and the like. Follow the instructions from Mailgun to get that set up.

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.

Example

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();
}