You are here

function mimemail_multipart_body in Mime Mail 7

Same name and namespace in other branches
  1. 5 mimemail.inc \mimemail_multipart_body()
  2. 6 mimemail.inc \mimemail_multipart_body()

Build 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.

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 mimemail_multipart_body()
mimemail_html_body in ./mimemail.inc
Generate a multipart message body with a text alternative for some HTML text.

File

./mimemail.inc, line 273
Common mail functions for sending e-mail. Originally written by Gerhard.

Code

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

  // Control variable to avoid boundary collision.
  static $part_num = 0;
  $boundary = sha1(uniqid($_SERVER['REQUEST_TIME'], TRUE)) . $part_num++;
  $body = '';
  $headers = array(
    'Content-Type' => "{$content_type}; boundary=\"{$boundary}\"",
  );
  if (!$sub_part) {
    $headers['MIME-Version'] = '1.0';
    $body = "This is a multi-part message in MIME format.\n";
  }
  foreach ($parts as $part) {
    $part_headers = array();
    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 (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'] . '"';
      }
    }
    else {
      if (!isset($part['Content-Transfer-Encoding'])) {
        $part_headers['Content-Transfer-Encoding'] = 'base64';
      }
      if (!isset($part['Content-Type'])) {
        $part['Content-Type'] = file_get_mimetype($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, variable_get('mimemail_crlf', "\n"));
      }
    }
    $body .= "\n--{$boundary}\n";
    $body .= mimemail_rfc_headers($part_headers) . "\n\n";
    $body .= isset($part_body) ? $part_body : '';
  }
  $body .= "\n--{$boundary}--\n";
  return array(
    'headers' => $headers,
    'body' => $body,
  );
}