You are here

public static function MimeMailFormatHelper::mimeMailMultipartBody in Mime Mail 8

Builds a multipart body.

Parameters

array $parts: An associative array containing the parts to be included:

  • name: A string containing the name of the attachment.
  • content: A string containing textual content.
  • file: A string containing file content.
  • Content-Type: A string containing the content type of either file or content. Mandatory for content, optional for file. If not present, it will be derived from file the file if mime_content_type is available. If not, application/octet-stream is used.
  • Content-Disposition: (optional) A string containing the disposition. Defaults to inline.
  • Content-Transfer-Encoding: (optional) Base64 is assumed for files, 8bit for other content.
  • Content-ID: (optional) for in-mail references to attachments.

Name is mandatory, one of content and file is required, they are mutually exclusive.

string $content_type: (optional) A string containing the content-type for the combined message. Defaults to multipart/mixed.

bool $sub_part: (optional) FALSE to return the entire body, TRUE to return only the body sub-part.

Return value

array An associative array containing the following elements:

  • body: A string containing the MIME-encoded multipart body of a mail.
  • headers: An array that includes some headers for the mail to be sent.
1 call to MimeMailFormatHelper::mimeMailMultipartBody()
MimeMailFormatHelper::mimeMailHtmlBody in src/Utility/MimeMailFormatHelper.php
Generates a multipart message body with a plaintext alternative.

File

src/Utility/MimeMailFormatHelper.php, line 572

Class

MimeMailFormatHelper
Utility methods for formatting MIME-encoded email messages.

Namespace

Drupal\mimemail\Utility

Code

public static function mimeMailMultipartBody(array $parts, $content_type = 'multipart/mixed; charset=utf-8', $sub_part = FALSE) {

  // Control variable to avoid boundary collision.
  static $part_num = 0;

  /** @var \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface $mime_type_guesser */
  $mime_type_guesser = \Drupal::service('file.mime_type.guesser');

  /** @var \Drupal\Component\Datetime\TimeInterface $time */
  $time = \Drupal::time();

  // Compute boundary hash.
  $request_time = $time
    ->getRequestTime();
  $boundary = sha1(uniqid($request_time, TRUE)) . $part_num++;

  // Header for mail body.
  $body = '';
  $headers = [
    'Content-Type' => "{$content_type}; boundary=\"{$boundary}\"",
  ];
  if (!$sub_part) {
    $headers['MIME-Version'] = '1.0';
    $body = 'This is a multi-part message in MIME format.' . static::CRLF;
  }

  // Part headers and contents.
  foreach ($parts as $part) {
    $part_headers = [];
    if (isset($part['Content-ID'])) {
      $part_headers['Content-ID'] = '<' . $part['Content-ID'] . '>';
    }
    if (isset($part['Content-Type'])) {
      $part_headers['Content-Type'] = $part['Content-Type'];
    }
    if (isset($part['Content-Disposition'])) {
      $part_headers['Content-Disposition'] = $part['Content-Disposition'];
    }
    elseif (mb_strpos($part['Content-Type'], 'multipart/alternative') === FALSE) {
      $part_headers['Content-Disposition'] = 'inline';
    }
    if (isset($part['Content-Transfer-Encoding'])) {
      $part_headers['Content-Transfer-Encoding'] = $part['Content-Transfer-Encoding'];
    }

    // Mail content provided as a string.
    if (isset($part['content']) && $part['content']) {
      if (!isset($part['Content-Transfer-Encoding'])) {
        $part_headers['Content-Transfer-Encoding'] = '8bit';
      }
      $part_body = $part['content'];
      if (isset($part['name'])) {
        $part_headers['Content-Type'] .= '; name="' . $part['name'] . '"';
        $part_headers['Content-Disposition'] .= '; filename="' . $part['name'] . '"';
      }

      // Mail content references in a filename.
    }
    else {
      if (!isset($part['Content-Transfer-Encoding'])) {
        $part_headers['Content-Transfer-Encoding'] = 'base64';
      }
      if (!isset($part['Content-Type'])) {
        $part['Content-Type'] = $mime_type_guesser
          ->guess($part['file']);
      }
      if (isset($part['name'])) {
        $part_headers['Content-Type'] .= '; name="' . $part['name'] . '"';
        $part_headers['Content-Disposition'] .= '; filename="' . $part['name'] . '"';
      }
      if (isset($part['file'])) {
        $file = is_file($part['file']) ? file_get_contents($part['file']) : $part['file'];
        $part_body = chunk_split(base64_encode($file), 76, static::CRLF);
      }
    }
    $body .= static::CRLF . "--{$boundary}" . static::CRLF;
    $body .= static::mimeMailRfcHeaders($part_headers) . static::CRLF;
    $body .= isset($part_body) ? $part_body : '';
  }
  $body .= static::CRLF . "--{$boundary}--" . static::CRLF;
  return [
    'headers' => $headers,
    'body' => $body,
  ];
}