Ejemplo n.º 1
0
 /**
  * @access public
  * @return string
  */
 public function __toString()
 {
     if ($this->headers->get('Content-Type') == null) {
         $this->headers->set('Content-Type', 'application/octet-stream');
     }
     $body = $this->body;
     if ($this->isMultiPart()) {
         $this->boundary = '--=_Part_' . md5(microtime());
         $this->headers->get('Content-Type')->param('boundary', $this->boundary);
         if ($body != '') {
             $body .= "\r\n\r\n";
         }
         foreach ($this->subparts as $subpart) {
             $body .= '--' . $this->boundary . "\r\n";
             $body .= !is_string($subpart) ? $subpart->__toString() : $subpart;
             $body .= "\r\n";
         }
         $body .= '--' . $this->boundary . "--\r\n";
     } else {
         if ($encoding = $this->headers->get('Content-Transfer-Encoding')) {
             $encoding = strtolower($encoding->value);
         }
         if (!in_array($encoding, array('7bit', '8bit', 'quoted-printable', 'base64', 'binary'))) {
             $this->headers->remove('Content-Transfer-Encoding');
             $encoding = '7bit';
         }
         switch ($encoding) {
             case 'quoted-printable':
                 /**
                  * Encodage en chaîne à guillemets
                  * 
                  * @see RFC 2045#6.7
                  */
                 $body = Mime::quotedPrintableEncode($body);
                 break;
             case 'base64':
                 /**
                  * Encodage en base64
                  * 
                  * @see RFC 2045#6.8
                  */
                 $body = rtrim(chunk_split(base64_encode($body)));
                 break;
             case '7bit':
             case '8bit':
                 $body = preg_replace("/\r\n?|\n/", "\r\n", $body);
                 /**
                  * Limitation sur les longueurs des lignes de texte.
                  * La limite basse est de 78 caractères par ligne.
                  * En tout état de cause, chaque ligne ne DOIT PAS
                  * faire plus de 998 caractères.
                  * 
                  * @see RFC 2822#2.1.1
                  */
                 $body = Mime::wordwrap($body, $this->wraptext ? 78 : 998);
                 break;
         }
     }
     return $this->headers->__toString() . "\r\n" . $body;
 }