You are here

function mimemail_multipart_body in Mime Mail 5

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

Parameters

$parts: an array of parts to be included each part is itself an array: array( 'name' => $name the name of the attachement 'content' => $content textual content 'file' => $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 file the file if mime_content_type is available. If not, application/octet-stream is used. 'Content-Disposition' => optional, inline is assumed 'Content-Transfer-Encoding' => optional, base64 is assumed for files 8bit for other content. 'Content-ID' => optional, for in-mail references to attachements. ) name is mandatory, one of content and file is required, they are mutually exclusive.

$content_type: Content-Type for the combined message, optional, default: multipart/mixed

Return value

an array containing the elements 'header' and 'body'. 'body' is the mime encoded multipart body of a mail. 'headers' is 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 186

Code

function mimemail_multipart_body($parts, $content_type = 'multipart/mixed; charset=utf-8', $sub_part = FALSE) {
  $boundary = md5(uniqid(time()));
  $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'];
    }
    else {
      $part_headers['Content-Disposition'] = 'inline';
    }
    if ($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'] = _mimemail_mimetype($part['file'], $type);
      }
      if (isset($part['name'])) {
        $part_headers['Content-Type'] .= '; name="' . $part['name'] . '"';
        $part_headers['Content-Disposition'] .= '; filename="' . $part['name'] . '"';
      }
      $part_body = chunk_split(base64_encode(file_get_contents($part['file'])));
    }
    $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,
  );
}