You are here

public function PHPMailer::WrapText in SMTP Authentication Support 7.2

Same name and namespace in other branches
  1. 5 smtp.module \PHPMailer::WrapText()
  2. 7 smtp.phpmailer.inc \PHPMailer::WrapText()

Wraps message for use with mailers that do not automatically perform wrapping and for quoted-printable. Original written by philippe.

@access public

Parameters

string $message The message to wrap:

integer $length The line length to wrap to:

boolean $qp_mode Whether to run in Quoted-Printable mode:

Return value

string

2 calls to PHPMailer::WrapText()
PHPMailer::EncodeHeader in ./smtp.phpmailer.inc
Encode a header string to best (shortest) of Q, B, quoted or none. @access public
PHPMailer::SetWordWrap in ./smtp.phpmailer.inc
Set the body wrapping. @access public

File

./smtp.phpmailer.inc, line 874
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 WrapText($message, $length, $qp_mode = FALSE) {
  $soft_break = $qp_mode ? sprintf(" =%s", $this->LE) : $this->LE;

  // If utf-8 encoding is used, we will need to make sure we don't
  // split multibyte characters when we wrap
  $is_utf8 = strtolower($this->CharSet) == "utf-8";
  $message = $this
    ->FixEOL($message);
  if (substr($message, -1) == $this->LE) {
    $message = substr($message, 0, -1);
  }
  $line = explode($this->LE, $message);
  $message = '';
  for ($i = 0; $i < count($line); $i++) {
    $line_part = explode(' ', $line[$i]);
    $buf = '';
    for ($e = 0; $e < count($line_part); $e++) {
      $word = $line_part[$e];
      if ($qp_mode and strlen($word) > $length) {
        $space_left = $length - strlen($buf) - 1;
        if ($e != 0) {
          if ($space_left > 20) {
            $len = $space_left;
            if ($is_utf8) {
              $len = $this
                ->UTF8CharBoundary($word, $len);
            }
            elseif (substr($word, $len - 1, 1) == "=") {
              $len--;
            }
            elseif (substr($word, $len - 2, 1) == "=") {
              $len -= 2;
            }
            $part = substr($word, 0, $len);
            $word = substr($word, $len);
            $buf .= ' ' . $part;
            $message .= $buf . sprintf("=%s", $this->LE);
          }
          else {
            $message .= $buf . $soft_break;
          }
          $buf = '';
        }
        while (strlen($word) > 0) {
          $len = $length;
          if ($is_utf8) {
            $len = $this
              ->UTF8CharBoundary($word, $len);
          }
          elseif (substr($word, $len - 1, 1) == "=") {
            $len--;
          }
          elseif (substr($word, $len - 2, 1) == "=") {
            $len -= 2;
          }
          $part = substr($word, 0, $len);
          $word = substr($word, $len);
          if (strlen($word) > 0) {
            $message .= $part . sprintf("=%s", $this->LE);
          }
          else {
            $buf = $part;
          }
        }
      }
      else {
        $buf_o = $buf;
        $buf .= $e == 0 ? $word : ' ' . $word;
        if (strlen($buf) > $length and $buf_o != '') {
          $message .= $buf_o . $soft_break;
          $buf = $word;
        }
      }
    }
    $message .= $buf . $this->LE;
  }
  return $message;
}