You are here

function mimemail_html_body in Mime Mail 5

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

Generate a multipart message body with a text alternative for some html text

Parameters

$body An HTML message body:

$subject The message subject:

$plaintext Whether the recipient prefers plaintext-only messages (default false):

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.

The first mime part is a multipart/alternative containing mime-encoded sub-parts for HTML and plaintext. Each subsequent part is the required image/attachment

1 call to mimemail_html_body()
mimemail_prepare in ./mimemail.module
Sends a mime-encoded e-mail.

File

./mimemail.inc, line 280

Code

function mimemail_html_body($body, $subject, $plaintext = FALSE, $text = NULL, $attachments = array()) {
  if (is_null($text)) {

    //generate plaintext alternative
    $text = mimemail_html_to_text($body);
  }
  if ($plaintext) {

    //Plain mail without attachment
    if (empty($attachments)) {
      return array(
        'body' => $text,
        'headers' => array(
          'Content-Type' => 'text/plain; charset=utf-8',
        ),
      );
    }
    else {
      $parts = array(
        array(
          'content' => $text,
          'Content-Type' => 'text/plain; charset=utf-8',
        ),
      );
    }
  }
  $content_type = 'multipart/alternative';
  $text_part = array(
    'Content-Type' => 'text/plain; charset=utf-8',
    'content' => $text,
  );

  //expand all local links
  $pattern = '/(<a[^>]+href=")([^"]*)/mi';
  $body = preg_replace_callback($pattern, '_mimemail_expand_links', $body);
  $mime_parts = mimemail_extract_files($body);
  $content = array(
    $text_part,
    array_shift($mime_parts),
  );
  $content = mimemail_multipart_body($content, $content_type, TRUE);
  $parts = array(
    array(
      'Content-Type' => $content['headers']['Content-Type'],
      'content' => $content['body'],
    ),
  );
  if ($mime_parts) {
    $content_type = 'multipart/related';
    $parts = array_merge($parts, $mime_parts);
  }
  foreach ($attachments as $a) {
    $a = (object) $a;
    $content_type = 'multipart/mixed';
    _mimemail_file($a->filepath, $a->filename, $a->filemime, 'attachment');
    $parts = array_merge($parts, _mimemail_file());
  }
  return mimemail_multipart_body($parts, "{$content_type}; charset=utf-8");
}