function mime_header_encode

Encodes MIME/HTTP header values that contain incorrectly encoded characters.

For example, mime_header_encode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".

See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.

Notes:

  • Only encode strings that contain non-ASCII characters.
  • We progressively cut-off a chunk with truncate_utf8(). This is to ensure each chunk starts and ends on a character boundary.
  • Using \n as the chunk separator may cause problems on some systems and may have to be changed to \r\n or \r.

Parameters

$string: The header to encode.

Return value

string The mime-encoded header.

See also

mime_header_decode()

4 calls to mime_header_encode()
DefaultMailSystem::mail in modules/system/system.mail.inc
Send an e-mail message, using Drupal variables and default settings.
drupal_mail_format_display_name in includes/mail.inc
Return a RFC-2822 compliant "display-name" component.
file_get_content_headers in includes/file.inc
Examines a file object and returns appropriate content headers for download.
file_module_test_file_download in modules/file/tests/file_module_test.module
Implements hook_file_download().

File

includes/unicode.inc, line 389

Code

function mime_header_encode($string) {
    if (preg_match('/[^\\x20-\\x7E]/', $string)) {
        $chunk_size = 47;
        // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
        $len = strlen($string);
        $output = '';
        while ($len > 0) {
            $chunk = drupal_truncate_bytes($string, $chunk_size);
            $output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
            $c = strlen($chunk);
            $string = substr($string, $c);
            $len -= $c;
        }
        return trim($output);
    }
    return $string;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.