You are here

function mimemail_multipart_body in Mime Mail 6

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

Helper function to build multipart messages.

Parameters

$parts: An array of arrays of parts to be included:

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

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

$content_type: A string containing the content-type for the combined message (optional).

Return value

An array containing the following elements:

  • headers: An array that includes some headers for the mail to be sent.
  • body: A string containing the mime encoded multipart body of a mail.
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 269
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(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'] . '"';
      }

      // 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'] = 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 .= $part_body;
  }
  $body .= "\n--{$boundary}--\n";
  return array(
    'headers' => $headers,
    'body' => $body,
  );
}