Skip to content

Commit

Permalink
Fixed intermittent QEmailServer encoding issue for Subject line
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeho committed Jul 18, 2011
1 parent 14c71c0 commit c390e3d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
36 changes: 12 additions & 24 deletions includes/qcodo/_core/framework/QEmailServer.class.php
Expand Up @@ -512,15 +512,15 @@ protected function CalculateMessageBody($strEncodingType, $strBoundary, $strAltB
$strBody .= sprintf("--%s\r\n", $strAltBoundary);
$strBody .= sprintf("Content-Type: text/plain; charset=\"%s\"\r\n", $strEncodingType);
$strBody .= sprintf("Content-Transfer-Encoding: quoted-printable\r\n\r\n");
$strBody .= self::QuotedPrintableEncode($this->Body);
$strBody .= QString::QuotedPrintableEncode($this->Body);
$strBody .= "\r\n\r\n";

// Provide Html Version of Email (if applicable)
if ($this->HtmlBody) {
$strBody .= sprintf("--%s\r\n", $strAltBoundary);
$strBody .= sprintf("Content-Type: text/html; charset=\"%s\"\r\n", $strEncodingType);
$strBody .= sprintf("Content-Transfer-Encoding: quoted-printable\r\n\r\n");
$strBody .= self::QuotedPrintableEncode($this->HtmlBody);
$strBody .= QString::QuotedPrintableEncode($this->HtmlBody);
$strBody .= "\r\n\r\n";
}

Expand All @@ -545,7 +545,7 @@ protected function CalculateMessageBody($strEncodingType, $strBoundary, $strAltB

// Plain-Text Version of the Body for Plain-Text Message Only
} else {
$strBody = self::QuotedPrintableEncode($this->Body);
$strBody = QString::QuotedPrintableEncode($this->Body);
}

return $strBody;
Expand Down Expand Up @@ -577,10 +577,15 @@ public function CalculateMessageHeaderAndBody($strEncodingType = null, QDateTime

// Additional "Optional" Headers
if ($this->Subject) {
$strSubject = self::QuotedPrintableEncode($this->Subject);
$strSubject = str_replace("=\r\n", "", $strSubject);
$strSubject = str_replace('?', '=3F', $strSubject);
$this->SetHeader('Subject', sprintf("=?%s?Q?%s?=", $strEncodingType, $strSubject));
// Encode to UTF8 Subject if Applicable
if (QString::IsContainsUtf8($this->Subject)) {
$strSubject = QString::QuotedPrintableEncode($this->Subject);
$strSubject = str_replace("=\r\n", "", $strSubject);
$strSubject = str_replace('?', '=3F', $strSubject);
$this->SetHeader('Subject', sprintf("=?%s?Q?%s?=", $strEncodingType, $strSubject));
} else {
$this->SetHeader('Subject', $this->Subject);
}
}
if ($this->Cc) $this->SetHeader('Cc', $this->Cc);

Expand Down Expand Up @@ -653,22 +658,5 @@ public function __set($strName, $mixValue) {
throw $objExc;
}
}

/**
* Encodes a given 8-bit string into a quoted-printable string,
* @param string $strString the string to encode
* @return string the encoded string
*/
public static function QuotedPrintableEncode($strString) {
if (function_exists('quoted_printable_encode')) {
$strText = quoted_printable_encode($strString);
} else {
$strText = preg_replace( '/[^\x21-\x3C\x3E-\x7E\x09\x20]/e', 'sprintf( "=%02X", ord ( "$0" ) ) ;', $strString );
preg_match_all( '/.{1,73}([^=]{0,2})?/', $strText, $arrMatch );
$strText = implode( '=' . "\r\n", $arrMatch[0] );
}

return $strText;
}
}
?>
36 changes: 36 additions & 0 deletions includes/qcodo/_core/framework/QString.class.php
Expand Up @@ -225,5 +225,41 @@ public static function IsLengthBeetween($strString, $intMinimumLength, $intMaxim
public static function ConvertToCamelCase($strString) {
return str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($strString))));
}

/**
* Encodes a given 8-bit string into a quoted-printable string,
* @param string $strString the string to encode
* @return string the encoded string
*/
public static function QuotedPrintableEncode($strString) {
if (function_exists('quoted_printable_encode')) {
$strText = quoted_printable_encode($strString);
} else {
$strText = preg_replace( '/[^\x21-\x3C\x3E-\x7E\x09\x20]/e', 'sprintf( "=%02X", ord ( "$0" ) ) ;', $strString );
preg_match_all( '/.{1,73}([^=]{0,2})?/', $strText, $arrMatch );
$strText = implode( '=' . "\r\n", $arrMatch[0] );
}

return $strText;
}

/**
* Returns whether or not the given string contains any UTF-8 encoded characters.
* Uses regexp pattern as originally defined from http://w3.org/International/questions/qa-forms-utf-8.html
* and modified by chris@w3style.co.uk for efficiency.
* @param string $strString
* @return boolean whether or not the string contains any UTF-8 characters
*/
public static function IsContainsUtf8($strString) {
return preg_match('%(?:
[\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
|\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
|\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
|\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
|[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
|\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)+%xs', $strString);
}
}
?>

0 comments on commit c390e3d

Please sign in to comment.