You are here

public function MailgunHandler::getDomains in Mailgun 8

Returns domains list from API.

Return value

array Returns the list of domains. Both array keys and values are domain names, E.g.: [ 'domain.name' => 'domain.name', ]

Overrides MailgunHandlerInterface::getDomains

File

src/MailgunHandler.php, line 133

Class

MailgunHandler
Mail handler to send out an email message array to the Mailgun API.

Namespace

Drupal\mailgun

Code

public function getDomains() {
  $domains = [];
  try {
    $result = $this->mailgun
      ->domains()
      ->index();

    // By default, limit is 100 domains, but we want to load all of them.
    if ($result
      ->getTotalCount() > 100) {
      $result = $this->mailgun
        ->domains()
        ->index($result
        ->getTotalCount());
    }
    foreach ($result
      ->getDomains() as $domain) {
      $domains[$domain
        ->getName()] = $domain
        ->getName();
    }
    ksort($domains);
  } catch (Exception $e) {
    $this->logger
      ->error('Could not retrieve domains from Mailgun API. @code: @message.', [
      '@code' => $e
        ->getCode(),
      '@message' => $e
        ->getMessage(),
    ]);
  }
  return $domains;
}