<?php

/**
 * @file
 * Hook implementations for the Mime Mail Example module.
 */

/**
 * Implements hook_theme().
 */
function mimemail_example_theme() {
  return [
    // Normally, theme suggestion templates are only found when they put in a
    // theme. This demonstrates how to provide a template with a custom module
    // by providing a new theme hook used when sending mail from this module.
    // The template use for this email is found in
    // templates/mimemail-message--mimemail-example.html.twig.
    'mimemail_message__mimemail_example' => [
      'render element' => 'elements',
      'base hook' => 'mimemail_message',
    ],
  ];
}

/**
 * Implements hook_mail().
 */
function mimemail_example_mail($key, &$message, $params) {
  // The $params array holds the values entered on the ExampleForm, stored
  // with the same structure as the $form array. We need to copy these values
  // to the appropriate place in the $message so that they get used when
  // sending the email.
  $message['from'] = isset($params['headers']['From']) ? $params['headers']['From'] : NULL;
  // Strip newline characters from e-mail subjects.
  $message['subject'] = isset($params['subject']) ? str_replace(["\r\n", "\r", "\n"], ' ', $params['subject']) : NULL;
  $message['body'][] = $params['body'];
}
