You are here

public function PHPMailer::UTF8CharBoundary in SMTP Authentication Support 7

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

Finds last character boundary prior to maxLength in a utf-8 quoted (printable) encoded string. Original written by Colin Brown. @access public

Parameters

string $encodedText utf-8 QP text:

int $maxLength find last character boundary prior to this length:

Return value

int

1 call to PHPMailer::UTF8CharBoundary()
PHPMailer::WrapText in ./smtp.phpmailer.inc
Wraps message for use with mailers that do not automatically perform wrapping and for quoted-printable. Original written by philippe.

File

./smtp.phpmailer.inc, line 975
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 UTF8CharBoundary($encodedText, $maxLength) {
  $foundSplitPos = FALSE;
  $lookBack = 3;
  while (!$foundSplitPos) {
    $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
    $encodedCharPos = strpos($lastChunk, "=");
    if ($encodedCharPos !== FALSE) {

      // Found start of encoded character byte within $lookBack block.
      // Check the encoded byte value (the 2 chars after the '=')
      $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
      $dec = hexdec($hex);
      if ($dec < 128) {

        // Single byte character.
        // If the encoded char was found at pos 0, it will fit
        // otherwise reduce maxLength to start of the encoded char
        $maxLength = $encodedCharPos == 0 ? $maxLength : $maxLength - ($lookBack - $encodedCharPos);
        $foundSplitPos = TRUE;
      }
      elseif ($dec >= 192) {

        // First byte of a multi byte character
        // Reduce maxLength to split at start of character
        $maxLength = $maxLength - ($lookBack - $encodedCharPos);
        $foundSplitPos = TRUE;
      }
      elseif ($dec < 192) {

        // Middle byte of a multi byte character, look further back
        $lookBack += 3;
      }
    }
    else {

      // No encoded character found
      $foundSplitPos = TRUE;
    }
  }
  return $maxLength;
}