You are here

function _views_send_prepare_mail in Views Send 8

Same name and namespace in other branches
  1. 6 views_send.module \_views_send_prepare_mail()
  2. 7 views_send.module \_views_send_prepare_mail()

Prepare the mail message before sending or spooling.

Parameters

array $message: which contains the following keys: from_name String holding the Sender's name. from_mail String holding the Sender's e-mail. to_name String holding the Recipient's name. to_mail String holding the Recipient's e-mail. subject String with the e-mail subject. This argument can be altered here. body Text with the e-mail body. This argument can be altered here. headers Associative array with e-mail headers. This argument can be altered here.

boolean $plain_format: Whether the e-mail should be sent in plain format.

array $attachments: An array with file information objects (as returned by file_save_upload).

2 calls to _views_send_prepare_mail()
views_send_batch_deliver in ./views_send.module
Preparing and sending a message (coming from a batch job).
views_send_queue_mail in ./views_send.module
Assembles the email and queues it for sending.

File

./views_send.module, line 772
The Views Send module.

Code

function _views_send_prepare_mail(&$message, $plain_format = TRUE, $attachments = array()) {

  // Extract all variables/keys from the message.
  extract($message);

  /**
   * TODO: In the future, this module will be able to send an existing node.
   * $key will have to make the difference. A value when we pickup a node, other
   * when user inputs the subject & body of the message.
   */
  $key = 'direct';

  // Build message parameters.
  $params = array();
  $params['from_name'] = $from_name;
  $params['from_mail'] = $from_mail;
  $params['from_formatted'] = _views_send_format_address($from_mail, $from_name);
  $params['to_name'] = $to_name;
  $params['to_mail'] = $to_mail;
  $to_mail_formatted = array();
  foreach (explode(',', $to_mail) as $addr) {
    $to_mail_formatted[] = _views_send_format_address($addr, $to_name);
  }
  $params['to_formatted'] = implode(', ', $to_mail_formatted);
  $params['subject'] = $subject;
  $params['body'] = $body;
  $params['headers'] = $headers;
  if (VIEWS_SEND_MIMEMAIL) {
    $params['attachments'] = [];
    foreach ($attachments as $attachment) {
      $params['attachments'][] = [
        'filepath' => $attachment
          ->getFileUri(),
        'filename' => $attachment
          ->getFilename(),
        'filemime' => $attachment
          ->getMimeType(),
      ];
    }
    if ($plain_format) {

      // Tell Mimemail module that this is a plain text message as HTML is the default.
      $params['plain'] = TRUE;
    }
  }

  // Call Drupal standard mail function, but without sending.
  $mail = \Drupal::service('plugin.manager.mail')
    ->mail('views_send', $key, $params['to_formatted'], \Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId(), $params, $params['from_formatted'], FALSE);

  // Updating message with data from generated mail
  $message['to_mail'] = $mail['to'];
  $message['from_mail'] = $mail['from'];
  $message['subject'] = $mail['subject'];
  $message['body'] = $mail['body'];
  $message['headers'] = serialize($mail['headers']);

  // Preserving attachments because Swift Mailer and Mandrill doesn't
  // handle attachments in the format function.
  if (!empty($params['attachments']) && (\Drupal::moduleHandler()
    ->moduleExists('mandrill') || \Drupal::moduleHandler()
    ->moduleExists('swiftmailer'))) {
    $attachments = array();
    if (\Drupal::moduleHandler()
      ->moduleExists('mandrill')) {
      foreach ($params['attachments'] as $attachment) {
        $attachments[] = [
          'uri' => $attachment['filepath'],
        ];
      }
    }
    else {
      if (\Drupal::moduleHandler()
        ->moduleExists('swiftmailer')) {
        $attachments = $params['attachments'];
      }
    }
    $message['params'] = array(
      'attachments' => $attachments,
    );
  }
}