You are here

function _views_send_prepare_mail in Views Send 7

Same name and namespace in other branches
  1. 8 views_send.module \_views_send_prepare_mail()
  2. 6 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 1071
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'] = $attachments;
    if ($plain_format) {
      $params['plain'] = TRUE;
    }
  }

  // Call Drupal standard mail function, but without sending.
  $mail = drupal_mail('views_send', $key, $params['to_formatted'], language_default(), $params, $params['from_formatted'], FALSE);

  // Add additional Mime Mail post processing.
  if (VIEWS_SEND_MIMEMAIL) {

    // We want to spool the Subject decoded.
    $mail['subject'] = mime_header_decode($mail['subject']);
  }

  // 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['send'] = $mail['send'];
  $message['headers'] = serialize($mail['headers']);

  // Preserving attachments because Swift Mailer and Mandrill doesn't
  // handle attachments in the format function.
  if (!empty($params['attachments']) && (module_exists('mandrill') || module_exists('swiftmailer'))) {
    $attachments = array();
    if (module_exists('mandrill')) {
      foreach ($params['attachments'] as $attachment) {
        $attachments[] = drupal_realpath($attachment['uri']);
      }
    }
    elseif (module_exists('swiftmailer')) {
      $attachments = $params['attachments'];
    }
    if (!empty($attachments)) {
      $message['params'] = array(
        'attachments' => $attachments,
      );
    }
  }
}