You are here

public function PHPMailer::Base64EncodeWrapMB in SMTP Authentication Support 7

Same name and namespace in other branches
  1. 7.2 smtp.phpmailer.inc \PHPMailer::Base64EncodeWrapMB()

Correctly encodes and wraps long multibyte strings for mail headers without breaking lines within a character. Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php @access public

Parameters

string $str multi-byte text to wrap encode:

Return value

string

1 call to PHPMailer::Base64EncodeWrapMB()
PHPMailer::EncodeHeader in ./smtp.phpmailer.inc
Encode a header string to best (shortest) of Q, B, quoted or none. @access public

File

./smtp.phpmailer.inc, line 1603
The mail handler class in smtp module, based on code of the phpmailer library, customized and relicensed to GPLv2.

Class

PHPMailer
PHPMailer - PHP email transport class NOTE: Requires PHP version 5 or later @package PHPMailer @author Andy Prevost @author Marcus Bointon @copyright 2004 - 2009 Andy Prevost

Code

public function Base64EncodeWrapMB($str) {
  $start = "=?" . $this->CharSet . "?B?";
  $end = "?=";
  $encoded = "";
  $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
  $offset = $avgLength = floor($length * $ratio * 0.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 . $this->LE;
  }

  // Chomp the last linefeed
  $encoded = substr($encoded, 0, -strlen($this->LE));
  return $encoded;
}