You are here

protected function MailgunMail::buildMessage in Mailgun 8

Builds the e-mail message in preparation to be sent to Mailgun.

Parameters

array $message: A message array, as described in hook_mail_alter(). $message['params'] may contain additional parameters.

Return value

array An email array formatted for Mailgun delivery.

See also

https://documentation.mailgun.com/en/latest/api-sending.html#sending

2 calls to MailgunMail::buildMessage()
MailgunMail::mail in src/Plugin/Mail/MailgunMail.php
Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail().
MailgunQueueMail::mail in src/Plugin/Mail/MailgunQueueMail.php
Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail().

File

src/Plugin/Mail/MailgunMail.php, line 177

Class

MailgunMail
Default Mailgun mail system plugin.

Namespace

Drupal\mailgun\Plugin\Mail

Code

protected function buildMessage(array $message) {

  // Build the Mailgun message array.
  $mailgun_message = [
    'from' => $message['headers']['From'],
    'to' => $message['to'],
    'subject' => $message['subject'],
    'html' => $message['body'],
  ];

  // Remove HTML version if the message does not support HTML.
  if (isset($message['params']['html']) && !$message['params']['html']) {
    unset($mailgun_message['html']);
  }

  // Set text version of the message.
  if (isset($message['plain'])) {
    $mailgun_message['text'] = $message['plain'];
  }
  else {
    $converter = new Html2Text($message['body'], [
      'width' => 0,
    ]);
    $mailgun_message['text'] = $converter
      ->getText();
  }

  // Add Cc / Bcc headers.
  if (!empty($message['headers']['Cc'])) {
    $mailgun_message['cc'] = $message['headers']['Cc'];
  }
  if (!empty($message['headers']['Bcc'])) {
    $mailgun_message['bcc'] = $message['headers']['Bcc'];
  }

  // Add Reply-To as header according to Mailgun API.
  if (!empty($message['reply-to'])) {
    $mailgun_message['h:Reply-To'] = $message['reply-to'];
  }

  // For a full list of allowed parameters,
  // see: https://documentation.mailgun.com/api-sending.html#sending.
  $allowed_params = [
    'o:tag',
    'o:campaign',
    'o:deliverytime',
    'o:dkim',
    'o:testmode',
    'o:tracking',
    'o:tracking-clicks',
    'o:tracking-opens',
  ];
  foreach ($message['params'] as $key => $value) {

    // Check if it's one of the known parameters.
    $allowed = in_array($key, $allowed_params) ? TRUE : FALSE;
    if ($allowed) {
      $mailgun_message[$key] = $value;
    }

    // Check for custom MIME headers or custom JSON data.
    if (substr($key, 0, 2) == 'h:' || substr($key, 0, 2) == 'v:') {
      $mailgun_message[$key] = $value;
    }
  }

  // Mailgun will accept the message but will not send it.
  if ($this->mailgunConfig
    ->get('test_mode')) {
    $mailgun_message['o:testmode'] = 'yes';
  }

  // Add default tags by mail key if enabled.
  if ($this->mailgunConfig
    ->get('tagging_mailkey')) {
    $mailgun_message['o:tag'][] = $message['id'];
  }

  // Make sure the files provided in the attachments array exist.
  if (!empty($message['params']['attachments'])) {
    $attachments = [];
    foreach ($message['params']['attachments'] as $attachment) {
      if (!empty($attachment['filepath']) && file_exists($attachment['filepath'])) {
        $attachments[] = [
          'filePath' => $attachment['filepath'],
        ];
      }
      elseif (!empty($attachment['filecontent']) && !empty($attachment['filename'])) {
        $attachments[] = [
          'fileContent' => $attachment['filecontent'],
          'filename' => $attachment['filename'],
        ];
      }
    }
    if (count($attachments) > 0) {
      $mailgun_message['attachment'] = $attachments;
    }
  }
  if ($this
    ->checkTracking($message)) {
    $track_opens = $this->mailgunConfig
      ->get('tracking_opens');
    if (!empty($track_opens)) {
      $mailgun_message['o:tracking-opens'] = $track_opens;
    }
    $track_clicks = $this->mailgunConfig
      ->get('tracking_clicks');
    if (!empty($track_clicks)) {
      $mailgun_message['o:tracking-clicks'] = $track_opens;
    }
  }
  else {
    $mailgun_message['o:tracking'] = 'no';
  }
  return $mailgun_message;
}