public function PHPMailer::base64EncodeWrapMB

5.x PHPMailer.php public PHPMailer::base64EncodeWrapMB($str, $linebreak = null)

Encode and wrap long multibyte strings for mail headers without breaking lines within a character. Adapted from a function by paravoid.

Parameters

string $str multi-byte text to wrap encode:

string $linebreak string to use as linefeed/end-of-line:

Return value

string

See also

http://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283

1 call to PHPMailer::base64EncodeWrapMB()
PHPMailer::encodeHeader in inc/PHPMailer/src/PHPMailer.php
Encode a header value (not including its label) optimally. Picks shortest of Q, B, or none. Result includes folding if needed. See RFC822 definitions for phrase, comment and text positions.

File

inc/PHPMailer/src/PHPMailer.php, line 3222

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public function base64EncodeWrapMB($str, $linebreak = null) 
 {
  $start = '=?' . $this->CharSet . '?B?';
  $end = '?=';
  $encoded = '';
  if (null === $linebreak) {
    $linebreak = static::$LE;
  }

  $mb_length = mb_strlen($str, $this->CharSet);
  // Each line must have length <= 75, including $start and $end
  $length = 75 - strlen($start) - strlen($end);
  // Average multi-byte ratio
  $ratio = $mb_length / strlen($str);
  // Base64 has a 4:3 ratio
  $avgLength = floor($length * $ratio * .75);

  for ($i = 0; $i < $mb_length; $i += $offset) {
    $lookBack = 0;
    do {
      $offset = $avgLength - $lookBack;
      $chunk = mb_substr($str, $i, $offset, $this->CharSet);
      $chunk = base64_encode($chunk);
      ++$lookBack;
    } while (strlen($chunk) > $length);
    $encoded .= $chunk . $linebreak;
  }

  // Chomp the last linefeed
  return substr($encoded, 0, -strlen($linebreak));
}