You are here

public function SWIFTMailSystem::format in Swift Mailer 7

Formats a message composed by drupal_mail().

Parameters

array $message: A message array holding all relevant details for the message.

Return value

string The message as it should be sent.

Overrides MailSystemInterface::format

See also

http://api.drupal.org/api/drupal/includes--mail.inc/interface/MailSystem...

File

includes/classes/SWIFTMailSystem.inc, line 21
The implementation of MailSystemInterface which delegates handling of e-mails to the Swift Mailer library.

Class

SWIFTMailSystem
@file The implementation of MailSystemInterface which delegates handling of e-mails to the Swift Mailer library.

Code

public function format(array $message) {
  if (!empty($message) && is_array($message)) {

    // Get default mail line endings and merge all lines in the e-mail body
    // separated by the mail line endings.
    $line_endings = variable_get('mail_line_endings', MAIL_LINE_ENDINGS);
    $message['body'] = implode($line_endings, $message['body']);

    // Get applicable format.
    $applicable_format = $this
      ->getApplicableFormat($message);

    // Theme message if format is set to be HTML.
    if ($applicable_format == SWIFTMAILER_FORMAT_HTML) {
      if (isset($message['params']['theme'])) {
        $message['body'] = theme($message['params']['theme'], $message);
      }
      else {
        $message['body'] = theme('swiftmailer', $message);
      }
      if (variable_get('swiftmailer_convert_mode', SWIFTMAILER_VARIABLE_CONVERT_MODE_DEFAULT) || !empty($message['params']['convert'])) {
        switch (variable_get('swiftmailer_convert_library')) {
          case SWIFTMAILER_VARIABLE_CONVERT_LIBRARY_MAILSYSTEM:
            $message['plain'] = mailsystem_html_to_text($message['body']);
            break;
          case SWIFTMAILER_VARIABLE_CONVERT_LIBRARY_DEFAULT:
          default:
            $converter = new Html2Text($message['body']);
            $message['plain'] = $converter
              ->getText();
            break;
        }
      }
    }

    // Process any images specified by 'image:' which are to be added later
    // in the process. All we do here is to alter the message so that image
    // paths are replaced with cid's. Each image gets added to the array
    // which keeps track of which images to embed in the e-mail.
    $embeddable_images = array();
    $processed_images = array();
    preg_match_all('/"image:([^"]+)"/', $message['body'], $embeddable_images);
    for ($i = 0; $i < count($embeddable_images[0]); $i++) {
      $image_id = $embeddable_images[0][$i];
      if (isset($processed_images[$image_id])) {
        continue;
      }
      $image_path = trim($embeddable_images[1][$i]);
      $image_name = basename($image_path);
      if (drupal_substr($image_path, 0, 1) == '/') {
        $image_path = drupal_substr($image_path, 1);
      }
      $image = new stdClass();
      $image->uri = $image_path;
      $image->filename = $image_name;
      $image->filemime = file_get_mimetype($image_path);
      $image->cid = rand(0, 9999999999);
      $message['params']['images'][] = $image;
      $message['body'] = preg_replace($image_id, 'cid:' . $image->cid, $message['body']);
      $processed_images[$image_id] = 1;
    }
    return $message;
  }
}