Ejemplo n.º 1
0
 /**
  * Encodes all non-encoded headers in the mail.
  * Also encodes mail body.
  *
  * @param string $mail        Mail with headers and body.
  * @param string $encoding    Target encoding (if not presented, autodetected).
  * @return string
  */
 function mailenc($mail, $encoding = null)
 {
     @(list($head, $body) = preg_split("/\r?\n\r?\n/s", $mail, 2));
     if (!$encoding) {
         $encoding = '';
         $re = '/^Content-type: \\s* \\S+ \\s*; \\s* charset \\s* = \\s* (\\S+)/mix';
         if (preg_match($re, $head, $p)) {
             $encoding = $p[1];
         }
     }
     $newhead = "";
     foreach (preg_split('/\\r?\\n/s', $head) as $line) {
         $line = Mail_Simple::_mailenc_header($line, $encoding);
         $newhead .= "{$line}\r\n";
     }
     // We ALWAYS use quoted-printable for mail body (not base64), because
     // seems Kaspersky KIS 2012 has a bug while processing base64-encoded
     // bodies when mail headers exceed a particular length (or it is a
     // ESET NOD32 Antivirus server module bug? who knows...).
     if (true) {
         $body = Mail_Simple::encodeQuotedPrintable($body);
         $newhead .= "Content-Transfer-Encoding: quoted-printable\r\n";
     } else {
         $body = chunk_split(base64_encode($body));
         $newhead .= "Content-Transfer-Encoding: base64\r\n";
     }
     return "{$newhead}\r\n{$body}";
 }