Esempio n. 1
0
 /**
  * Get the data in the format suitable for sending
  * @return Swift_Cache_OutputStream
  * @throws Swift_FileException If the file stream given cannot be read
  * @throws Swift_Message_MimeException If some required headers have been forcefully removed
  */
 public function buildData()
 {
     Swift_ClassLoader::load("Swift_Message_Encoder");
     Swift_ClassLoader::load("Swift_Cache_JointOutputStream");
     if (!empty($this->children)) {
         if ($this->boundary === null) {
             $this->boundary = self::generateBoundary();
         }
         $this->headers->setAttribute("Content-Type", "boundary", $this->boundary);
         $this->cache->clear("append");
         foreach ($this->children as $part) {
             $this->cache->write("append", $this->LE . "--" . $this->boundary . $this->LE);
             $part_stream = $part->build();
             while (false !== ($bytes = $part_stream->read())) {
                 $this->cache->write("append", $bytes);
             }
         }
         $this->cache->write("append", $this->LE . "--" . $this->boundary . "--" . $this->LE);
     }
     $joint_os = new Swift_Cache_JointOutputStream();
     //Try using a cached version to save some cycles (at the expense of memory)
     //if ($this->cache !== null) return $this->cache . $append;
     if ($this->cache->has("body")) {
         $joint_os->addStream($this->cache->getOutputStream("body"));
         $joint_os->addStream($this->cache->getOutputStream("append"));
         return $joint_os;
     }
     $is_file = $this->getData() instanceof Swift_File;
     switch ($this->getEncoding()) {
         case "quoted-printable":
             if ($is_file) {
                 $qp_os = Swift_Message_Encoder::instance()->QPEncodeFile($this->getData(), 76, $this->LE);
                 while (false !== ($bytes = $qp_os->read())) {
                     $this->cache->write("body", $bytes);
                 }
             } else {
                 $this->cache->write("body", Swift_Message_Encoder::instance()->QPEncode($this->getData(), 76, 0, false, $this->LE));
             }
             break;
         case "base64":
             if ($is_file) {
                 $b64_os = Swift_Message_Encoder::instance()->base64EncodeFile($this->getData(), 76, $this->LE);
                 while (false !== ($bytes = $b64_os->read())) {
                     $this->cache->write("body", $bytes);
                 }
             } else {
                 $this->cache->write("body", Swift_Message_Encoder::instance()->base64Encode($this->getData(), 76, 0, false, $this->LE));
             }
             break;
         case "binary":
             if ($is_file) {
                 $data = $this->getData();
                 while (false !== ($bytes = $data->read(8192))) {
                     $this->cache->write("body", $bytes);
                 }
             } else {
                 $this->cache->write("body", $this->getData());
             }
             break;
         case "7bit":
             if ($is_file) {
                 $os = Swift_Message_Encoder::instance()->encode7BitFile($this->getData(), $this->wrap, $this->LE);
                 while (false !== ($bytes = $os->read())) {
                     $this->cache->write("body", $bytes);
                 }
             } else {
                 $this->cache->write("body", Swift_Message_Encoder::instance()->encode7Bit($this->getData(), $this->wrap, $this->LE));
             }
             break;
         case "8bit":
         default:
             if ($is_file) {
                 $os = Swift_Message_Encoder::instance()->encode8BitFile($this->getData(), $this->wrap, $this->LE);
                 while (false !== ($bytes = $os->read())) {
                     $this->cache->write("body", $bytes);
                 }
             } else {
                 $this->cache->write("body", Swift_Message_Encoder::instance()->encode8Bit($this->getData(), $this->wrap, $this->LE));
             }
             break;
     }
     $joint_os->addStream($this->cache->getOutputStream("body"));
     $joint_os->addStream($this->cache->getOutputStream("append"));
     return $joint_os;
 }
 public function testLongListsOfAttributesAreBrokenOntoNewLines()
 {
     $headers = new Swift_Message_Headers();
     $headers->set("Foo", "xxx");
     $headers->setAttribute("Foo", "bar", "yyy");
     $headers->setAttribute("Foo", "zip", "498567hgdjwbvi");
     $headers->setAttribute("Foo", "verylong", "asfsafvasdbfouiwebgjwkebvfewivg");
     $structure = $headers->build();
     $this->assertEqual("Foo: xxx; bar=yyy; zip=498567hgdjwbvi;\r\n verylong=asfsafvasdbfouiwebgjwkebvfewivg", $structure);
 }