You are here

function PHPMailer::EncodeHeader in SMTP Authentication Support 5

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

Encode a header string to best of Q, B, quoted or none. @access private

Return value

string

3 calls to PHPMailer::EncodeHeader()
PHPMailer::AddrFormat in ./smtp.module
Formats an address correctly. @access private
PHPMailer::CreateHeader in ./smtp.module
Assembles message header. @access private
PHPMailer::MailSend in ./smtp.module
Sends mail using the PHP mail() function. @access private

File

./smtp.module, line 1611
Enables drupal to send email directly to an SMTP server using authentication. Uses the PHPMailer class by Brent R. Matzelle.

Class

PHPMailer
PHPMailer - PHP email transport class @package PHPMailer @author Brent R. Matzelle @copyright 2001 - 2003 Brent R. Matzelle

Code

function EncodeHeader($str, $position = 'text') {
  $x = 0;
  switch (strtolower($position)) {
    case 'phrase':
      if (!preg_match('/[\\200-\\377]/', $str)) {

        // Can't use addslashes as we don't know what value has magic_quotes_sybase.
        $encoded = addcslashes($str, "\0..\37\\\"");
        if ($str == $encoded && !preg_match('/[^A-Za-z0-9!#$%&\'*+\\/=?^_`{|}~ -]/', $str)) {
          return $encoded;
        }
        else {
          return "\"{$encoded}\"";
        }
      }
      $x = preg_match_all('/[^\\040\\041\\043-\\133\\135-\\176]/', $str, $matches);
      break;
    case 'comment':
      $x = preg_match_all('/[()"]/', $str, $matches);

    // Fall-through
    case 'text':
    default:
      $x += preg_match_all('/[\\000-\\010\\013\\014\\016-\\037\\177-\\377]/', $str, $matches);
      break;
  }
  if ($x == 0) {
    return $str;
  }
  $maxlen = 75 - 7 - strlen($this->CharSet);

  // Try to select the encoding which should produce the shortest output
  if (strlen($str) / 3 < $x) {
    $encoding = 'B';
    $encoded = base64_encode($str);
    $maxlen -= $maxlen % 4;
    $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  }
  else {
    $encoding = 'Q';
    $encoded = $this
      ->EncodeQ($str, $position);
    $encoded = $this
      ->WrapText($encoded, $maxlen, true);
    $encoded = str_replace("=" . $this->LE, "\n", trim($encoded));
  }
  $encoded = preg_replace('/^(.*)$/m', " =?" . $this->CharSet . "?{$encoding}?\\1?=", $encoded);
  $encoded = trim(str_replace("\n", $this->LE, $encoded));
  return $encoded;
}