|
 |
imap_8bit (PHP 3, PHP 4, PHP 5) imap_8bit --
Convert an 8bit string to a quoted-printable string
Descriptionstring imap_8bit ( string string )
Convert an 8bit string to a quoted-printable string (according to
RFC2045, section
6.7).
Returns a quoted-printable string.
See also imap_qprint().
imap_8bit
umu
28-Jan-2006 05:58
...deed.ztinmehc-ut.zrh@umuumu@hrz.tu-chemnitz.deed...
I use the following function instead of imap_8bit
when using PHP without the IMAP module,
which is based on code found in
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.
<?php
function quoted_printable_encode($sText,$bEmulate_imap_8bit=true) {
$aLines=explode(chr(13).chr(10),$sText);
for ($i=0;$i<count($aLines);$i++) {
$sLine =& $aLines[$i];
if (strlen($sLine)===0) continue; $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
if ($bEmulate_imap_8bit)
$sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
$sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
$sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
{
$iLength = strlen($sLine);
$iLastChar = ord($sLine{$iLength-1});
if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
if (($iLastChar==0x09)||($iLastChar==0x20)) {
$sLine{$iLength-1}='=';
$sLine .= ($iLastChar==0x09)?'09':'20';
}
} if ($bEmulate_imap_8bit) {
$sLine=str_replace(' =0D','=20=0D',$sLine);
}
preg_match_all( '/.{1,73}([^=]{0,2})?/', $sLine, $aMatch );
$sLine = implode( '=' . chr(13).chr(10), $aMatch[0] ); }
return implode(chr(13).chr(10),$aLines);
}
?>
hans at lintoo dot dk
22-Dec-2005 06:05
The code below this note gives a lot of trouple with SpamAssasin, to fix it you will need to encode your subject with something like this:
<?php
private function encodeHeader($input, $charset = 'ISO-8859-1')
{
preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches);
foreach ($matches[1] as $value) {
$replacement = preg_replace('/([\x20\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
$input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
}
return wordwrap($input,75,"\n\t",true);
}
?>
hans at lintoo dot dk
04-Oct-2005 10:35
I modified nick at plumdigitalmedia dot com's note so that it may support ISO-8859-1 encoded headers:
<?php
public function encodeSubject($string,$prefix="=?ISO-8859-1?Q?",$postfix="?=") {
$crlf = "\n\t";
$string = preg_replace('!(\r\n|\r|\n)!', $crlf, $string) . $crlf ;
$f[] = '/([\000-\010\013\014\016-\037\075\177-\377])/e' ;
$r[] = "'=' . sprintf('%02X', ord('\\1'))" ;
$f[] = '/([\011\040])' . $crlf . '/e' ;
$r[] = "'=' . sprintf('%02X', ord('\\1')) . '" . $crlf . "'" ;
$string = preg_replace($f, $r, $string);
return $prefix.trim(wordwrap($string, 70 - strlen($prefix) - strlen($postfix), ' ' . $postfix . $crlf . $prefix, true)).$postfix;
}
?>
Regards, Hans @ http://lintoo.dk/
marco dot manngatter at ecg-leipzig dot de
15-Sep-2005 03:43
you can use the following to make a larger subject.
use the imap_qprint() function, to convert your subject.
example:
<?php
$subject .= "=?iso-8859-1?Q?" . imap_qprint($subject) . "?=";
?>
nick at plumdigitalmedia dot com
10-May-2005 08:15
This function appears to wrap lines in the middle of words, not just at whitespace, which upsets some versions of Outlook Express when used to format email body text. We've had more luck with this function:
<?
function quoted_printable($string)
{
$crlf = "\n" ;
$string = preg_replace('!(\r\n|\r|\n)!', $crlf, $string) . $crlf ;
$f[] = '/([\000-\010\013\014\016-\037\075\177-\377])/e' ;
$r[] = "'=' . sprintf('%02X', ord('\\1'))" ;
$f[] = '/([\011\040])' . $crlf . '/e' ;
$r[] = "'=' . sprintf('%02X', ord('\\1')) . '" . $crlf . "'" ;
$string = preg_replace($f, $r, $string) ;
return trim(wordwrap($string, 70, ' =' . $crlf)) ;
}
?>
k dot kozlowski at enter dot pl
14-Mar-2005 06:50
I had problems with encoding large subjects with polish characters. The problem was that imap_8bit() splits subject (on 75-th char) but when I add "=?ISO-8859-2?Q?" the header is too long.
This is a solution :
<?php
$subject = str_replace(" ", "_", trim($subject)) ;
$subject = str_replace("?", "=3F", str_replace("=\r\n", "", imap_8bit($subject))) ;
$subject = str_replace("\r\n", "?=\r\n =?ISO-8859-2?Q?", chunk_split($subject, 55)) ;
$subject = "=?ISO-8859-2?Q?" . substr($subject, 0, strlen($subject)-20) . "?=" ;
?>
rayFX
24-Jun-2004 09:14
imap_8bit seems to have a bug as it doesn't encode "?".
Had a lot of trouble to encode a mail-subject with
german umlaute with an ending "?"...
Try this:
$subject = "=?iso-8859-1?Q?" . str_replace("?","=3F",imap_8bit($tsubject_to_encode)) . "?=";
MagicalTux at FF.ST
15-Dec-2003 06:50
Reading Bully's note :
you can use the following to make a larger subject:
<?
$encoded = str_replace("=\r\n","",imap_8bit($string_to_encode));
?>
It's wrong. The header MAY NOT go over 75 chars per line.
The right solution :
<?
$encoded = rtrim( str_replace( "\n","\n\t", imap_8bit ($string_to_encode)))."\r\n";
?>
It will add \t at the start of the encoded new lines.
Mailers to see the lines after Subject: as extensions of this header.
php dot net at werner-ott dot de
09-Dec-2002 02:45
A comment on the "split-after-75-characters" phenomenon:
By splitting lines up after 75 characters, the function's behaviour is complying to RFC2047 (http://www.ietf.org/rfc/rfc2047.txt) (which specifies a protocol for the representation of non-ASCII text in message headers), section "2. Syntax of encoded-words".
A so called 'encoded word' has the following format:
encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
An 'encoded-word' may not be more than 75 characters long, including 'charset', 'encoding', 'encoded-text', and delimiters. If it is desirable to encode more text than will fit in an 'encoded-word' of 75 characters, multiple 'encoded-word's (separated by CRLF SPACE) may be used.
hth
WOT
bully at newearth dot de
24-Apr-2002 07:48
you can use the following to make a larger subject:
$encoded = str_replace("=\r\n","",imap_8bit($string_to_encode));
zuffa at isdd dot sk
23-Sep-2000 08:57
Warning !
This function splits input text into
several lines aligned to 75 characters.
This is critical when you need input
text to be only one striaght line as
e.g. in e-mail header values.
| |