You are here

public function MailgunHandler::getDomain in Mailgun 8

Parses and returns domain based on the email "From" value.

Parameters

string $from: "From" parameter of the mail message.

Return value

string|bool Returns domain name or FALSE if we couldn't parse it.

Overrides MailgunHandlerInterface::getDomain

1 call to MailgunHandler::getDomain()
MailgunHandler::sendMail in src/MailgunHandler.php
Connects to Mailgun API and sends out the email.

File

src/MailgunHandler.php, line 163

Class

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

Namespace

Drupal\mailgun

Code

public function getDomain($from) {
  $domain = $this->mailgunConfig
    ->get('working_domain');
  if ($domain !== '_sender') {
    return $domain;
  }
  $emailParser = new EmailParser(new EmailLexer());
  if ($this->emailValidator
    ->isValid($from)) {
    return $emailParser
      ->parse($from)['domain'];
  }

  // Extract the domain from the sender's email address.
  // Use regular expression to check since it could be either a plain email
  // address or in the form "Name <example@example.com>".
  $tokens = preg_match('/^\\s*(.+?)\\s*<\\s*([^>]+)\\s*>$/', $from, $matches) === 1 ? explode('@', $matches[2]) : explode('@', $from);
  return !empty($tokens) ? array_pop($tokens) : FALSE;
}