}
private function mimeencode($fieldtext, $stringescape = false) {
- $mText = imap_8bit($fieldtext);
+ if (function_exists('imap_8bit')) {
+ $mText = imap_8bit($fieldtext);
+ }
+ else {
+ $mText = quoted_printable_encode($fieldtext);
+ }
$is_qpformat = ($mText != $fieldtext);
if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
// if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
}
return $mText;
}
+
+ private function quoted_printable_encode($sText, $bEmulate_imap_8bit=true) {
+ /* by ...deed.ztinmehc-ut.zrh@umuumu@hrz.tu-chemnitz.deed...
+ from https://secure.php.net/manual/en/function.imap-8bit.php#61216
+
+ I use the following function instead of imap_8bit
+ when using PHP without the IMAP module,
+ which is based on code found in
+ http://www.php.net/quoted_printable_decode,
+ and giving (supposedly) exactly the same results as imap_8bit,
+ (tested on thousands of random strings containing lots
+ of spaces, tabs, crlf, lfcr, lf, cr and so on,
+ no counterexample found SO FAR:)
+
+ AND you can force a trailing space to be encoded,
+ as opposed to what imap_8bit does,
+ which I consider is a violation of RFC2045,
+ (see http://bugs.php.net/bug.php?id=35290)
+ by commenting that one central line.
+ */
+ // split text into lines
+ $aLines=explode(chr(13).chr(10),$sText);
+
+ for ($i=0;$i<count($aLines);$i++) {
+ $sLine =& $aLines[$i];
+ if (strlen($sLine)===0) continue; // do nothing, if empty
+
+ $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
+
+ // imap_8bit encodes x09 everywhere, not only at lineends,
+ // for EBCDIC safeness encode !"#$@[\]^`{|}~,
+ // for complete safeness encode every character :)
+ if ($bEmulate_imap_8bit)
+ $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
+
+ $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
+ $sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
+
+ // encode x09,x20 at lineends
+ {
+ $iLength = strlen($sLine);
+ $iLastChar = ord($sLine{$iLength-1});
+
+ // !!!!!!!!
+ // imap_8_bit does not encode x20 at the very end of a text,
+ // here is, where I don't agree with imap_8_bit,
+ // please correct me, if I'm wrong,
+ // or comment next line for RFC2045 conformance, if you like
+ if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
+
+ if (($iLastChar==0x09)||($iLastChar==0x20)) {
+ $sLine{$iLength-1}='=';
+ $sLine .= ($iLastChar==0x09)?'09':'20';
+ }
+ } // imap_8bit encodes x20 before chr(13), too
+ // although IMHO not requested by RFC2045, why not do it safer :)
+ // and why not encode any x20 around chr(10) or chr(13)
+ if ($bEmulate_imap_8bit) {
+ $sLine=str_replace(' =0D','=20=0D',$sLine);
+ //$sLine=str_replace(' =0A','=20=0A',$sLine);
+ //$sLine=str_replace('=0D ','=0D=20',$sLine);
+ //$sLine=str_replace('=0A ','=0A=20',$sLine);
+ }
+
+ // finally split into softlines no longer than 76 chars,
+ // for even more safeness one could encode x09,x20
+ // at the very first character of the line
+ // and after soft linebreaks, as well,
+ // but this wouldn't be caught by such an easy RegExp
+ preg_match_all( '/.{1,73}([^=]{0,2})?/', $sLine, $aMatch );
+ $sLine = implode( '=' . chr(13).chr(10), $aMatch[0] ); // add soft crlf's
+ }
+
+ // join lines into text
+ return implode(chr(13).chr(10),$aLines);
+ }
}
?>