You are here

function contact_storage_mail_alter in Contact Storage 8

Implements hook_mail_alter().

File

./contact_storage.module, line 412
Contains main module logic.

Code

function contact_storage_mail_alter(&$message) {

  // Check that the message isn't a copy sent to the sender (page_copy).
  if ($message['key'] == 'page_mail' && isset($message['params']['contact_message'])) {
    $contact_message = $message['params']['contact_message'];
    foreach ($contact_message
      ->getFields() as $field) {
      if ($field
        ->getFieldDefinition()
        ->getType() === 'contact_storage_options_email') {

        // Add recipients to the message from the Option email field.
        foreach ($field as $delta => $item) {
          $label = $item->value;

          // Obtain the email to add to the message, using the label.
          $email = $item
            ->getFieldDefinition()
            ->getSetting('allowed_values')[$label]['emails'];
          $message['to'] .= ',' . $email;
        }
      }
    }
  }
  if ($message['module'] === 'contact' && isset($message['params']['contact_message'])) {
    if ($message['key'] == 'page_autoreply') {
      $contact_form = $message['params']['contact_form'];

      // Filters the auto-reply message using the chosen format and sets it.
      if ($reply = $contact_form
        ->getReply()) {
        $filtered_reply = check_markup($contact_form
          ->getReply(), $contact_form
          ->getThirdPartySetting('contact_storage', 'page_autoreply_format', 'plain_text'));
        $message['body'] = [
          $filtered_reply,
        ];
      }
    }

    // Enforce that we are sending mails as html, if enabled, and tell
    // Swiftmailer to generate a plain text version.
    if (\Drupal::config('contact_storage.settings')
      ->get('send_html')) {
      $message['headers']['Content-Type'] = 'text/html';
      $message['params']['convert'] = TRUE;
    }
  }
}