伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: imap_8bit

来源:网络转载 浏览:49次 时间:2023-07-14
imap_alerts » « IMAP 函数 PHP 手册 函数参考 邮件相关扩展 IMAP IMAP 函数

imap_8bit

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_8bit — Convert an 8bit string to a quoted-printable string

说明

imap_8bit(string $string): string|false

Convert an 8bit string to a quoted-printable string (according to » RFC2045, section 6.7).

参数

string

The 8bit string to convert

返回值

Returns a quoted-printable string, 或者在失败时返回 false.

参见

imap_qprint() - Convert a quoted-printable string to an 8 bit string
add a note

User Contributed Notes 16 notes

up down 3 umu16 years ago ...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
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.

<?php
function quoted_printable_encode($sText,$bEmulate_imap_8bit=true) {
  // 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);
}
?>
up down 3 rayFX17 years ago 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)) . "?=";
up down 1 roman at REMOVETHIS dot teamnet dot com dot pl15 years ago Code based on k dot kozlowski at enter dot pl but for UTF-8
(the only problem i encounter is SUBJECT_ENCODED_TWICE on SPAM test with very long subject, but the same produce others MUA's)
<?
function header_quoted_printable_encode($string, $encoding='UTF-8') {
  
    $string = str_replace(" ", "_", trim($string)) ;
    // We need to delete "=\r\n" produced by imap_8bit() and replace '?'
    $string = str_replace("?", "=3F", str_replace("=\r\n", "", imap_8bit($string))) ;
   
    // Now we split by \r\n - i'm not sure about how many chars (header name counts or not?)
    $string = chunk_split($string, 73);
    // We also have to remove last unneeded \r\n :
    $string = substr($string, 0, strlen($string)-2);
    // replace newlines with encoding text "=?UTF ..."
    $string = str_replace("\r\n", "?=".HEAD_CRLF." =?".$encoding."?Q?", $string) ;
   
    return '=?'.$encoding.'?Q?'.$string.'?=';
}
?>
up down 1 nick at plumdigitalmedia dot com17 years ago 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)) ;
    }

?>
up down 2 Michael Konecny11 years ago There is a bug in MS Exchange server, that improperly handles CRLFs. Seems like it converts both CR and LF into LF, so, instead of getting just a new line, you get TWO new lines.
The header information is then improperly parsed by the e-mail client, and is usually viewed in the message body.

This can make custom-created headers unusable.

The only way I found to get around this is to go against RFC rules for header formatting, and use only \n for new lines.

Unfortunately I haven't been able to learn the version of Exchange server where this happened, but I have the feeling that it's not just the one I was unlucky to encounter.

Hope this helps someone.
up down 1 hans at lintoo dot dk16 years ago 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/
up down 1 k dot kozlowski at enter dot pl17 years ago 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)) ;
// We need to delete "=\r\n" produced by imap_8bit() and replace '?'
$subject = str_replace("?", "=3F", str_replace("=\r\n", "", imap_8bit($subject))) ;
// Now we split by \r\n but with encoding text "=?ISO ..."
$subject = str_replace("\r\n", "?=\r\n =?ISO-8859-2?Q?", chunk_split($subject, 55)) ;
// We also have to remove last unneeded encoding text :
$subject = "=?ISO-8859-2?Q?" . substr($subject, 0, strlen($subject)-20) . "?=" ;
?>
up down 2 Anonymous14 years ago umu's code seems to work great. BUT if you are on a Unix system you should replace all chr(13).chr(10) with just chr(10)

Works for me anyway on php5.mail() -> qmail@suse10.1 -> outlook express
up down 1 MagicalTux at FF.ST18 years ago 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.
up down 0 dave at jufer dot info6 years ago Encode a mail-subject-string to a encoded-word according to RFC2047 with awareness of special characters and total-linelength of 75.
Example: "Subject Testä" -> "=?ISO-8859-1?Q?Subject_Test=E4?="
<?php
function getSubjectEncoded($_str,$_charset='ISO-8859-1'){
    // 27.08.2015 david jufer
    // Convert an 8bit string to a encoded-word string (according to RFC2047) www.ietf.org/rfc/rfc2047.txt
    // encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
    // An 'encoded-word' may not be more than 75 characters long, including 'charset', 'encoding', 'encoded-text', and delimiters.
    // The "Q" encoding is similar to the "Quoted-Printable" content-transfer-encoding defined in RFC 2045
    $ret = '';
    $str = imap_8bit($_str);
    $len = 7+strlen($_charset);
    // remove linebreaks from imap_8bit, string chunks are too long
    $str = str_replace("=\r\n",'',$str);
    // insert linebreaks at right position, be aware of special chars ("ä" "=E4") do not split them
    $s = 0;
    $l = 75-$len;
    while(strlen($str)>$s){
        $ts = $s;
        if(substr($str,($s+$l)-2,1)=='='){
            $s += $l-2;
        }else if(substr($str,($s+$l)-1,1)=='='){
            $s += $l-1;
        }else{
            $s += $l;
        }
        $ret .= "\r\n".substr($str,$ts,$s-$ts);
    }
    // remove first linebreak, replace SPACES with "_"
    $ret = str_replace(' ','_',trim($ret));
    // put together encoded-word
    $ret = '=?'.$_charset.'?Q?'.str_replace("\r\n","?=\r\n =?".$_charset."?Q?",$ret)."?=";
    // rückgabe
    return $ret;
}
?>
up down 0 esh dot software at energiash dot com12 years ago When producing quoted printable emails this is the right way

$subject="Subject: =?iso-8859-1?Q?". imap_8bit("áéíóú"). "?=";

and the following would do the wrong thing, because qprint over "=ED" would produce 8bit data ( í ):

$subject="Subject: =?iso-8859-1?Q?". imap_qprint("=ED"). "?=";
up down 0 marco dot manngatter at ecg-leipzig dot de16 years ago 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) . "?=";

?>
up down 0 php dot net at werner-ott dot de19 years ago 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
up down 0 bully at newearth dot de20 years ago you can use the following to make a larger subject:

$encoded = str_replace("=\r\n","",imap_8bit($string_to_encode));
up down 0 zuffa at isdd dot sk21 years ago 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.
up down -2 hans at lintoo dot dk16 years ago 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
/**
* Function to encode a header if necessary
* according to RFC2047
*/
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);
}
?>
add a note

官方地址:https://www.php.net/manual/en/function.imap-8bit.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net