public function testGenerate()
 {
     $msg = new Zend_Mime_Message();
     // No Parts
     $p1 = new Zend_Mime_Part('This is a test');
     $p2 = new Zend_Mime_Part('This is another test');
     $msg->addPart($p1);
     $msg->addPart($p2);
     $res = $msg->generateMessage();
     $mime = $msg->getMime();
     $boundary = $mime->boundary();
     $p1 = strpos($res, $boundary);
     // $boundary must appear once for every mime part
     $this->assertTrue($p1 !== false);
     if ($p1) {
         $p2 = strpos($res, $boundary, $p1 + strlen($boundary));
         $this->assertTrue($p2 !== false);
     }
     // check if the two test messages appear:
     $this->assertTrue(strpos($res, 'This is a test') !== false);
     $this->assertTrue(strpos($res, 'This is another test') !== false);
     // ... more in ZMailTest
 }
示例#2
0
 /**
  * Return the MIME multipart representation of this MediaEntry.
  *
  * @return string The MIME multipart representation of this MediaEntry
  */
 public function encode()
 {
     $xmlData = $this->saveXML();
     if ($this->getMediaSource() === null) {
         // No attachment, just send XML for entry
         return $xmlData;
     } else {
         $mimeMessage = new Zend_Mime_Message();
         $mimeMessage->setMime($this->_mime);
         $xmlPart = new Zend_Mime_Part($xmlData);
         $xmlPart->type = 'application/atom+xml';
         $xmlPart->encoding = null;
         $mimeMessage->addPart($xmlPart);
         $binaryPart = new Zend_Mime_Part($this->getMediaSource()->encode());
         $binaryPart->type = $this->getMediaSource()->getContentType();
         $binaryPart->encoding = null;
         $mimeMessage->addPart($binaryPart);
         return $mimeMessage->generateMessage();
     }
 }
示例#3
0
 /**
  * Decodes a MIME encoded string and returns a Zend_Mime_Message object with
  * all the MIME parts set according to the given string
  *
  * @param string $message
  * @param string $boundary
  * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
  * @return Zend_Mime_Message
  */
 public static function createFromMessage($message, $boundary, $EOL = Zend_Mime::LINEEND)
 {
     $partsStr = self::_disassembleMime($message, $boundary);
     if (count($partsStr) <= 0) {
         return null;
     }
     $res = new Zend_Mime_Message();
     foreach ($partsStr as $part) {
         // separate header and body
         $header = true;
         // expecting header lines first
         $headersfound = array();
         $body = '';
         $lastheader = '';
         $lines = explode("\n", $part);
         // read line by line
         foreach ($lines as $line) {
             $line = trim($line);
             if ($header) {
                 if ($line == '') {
                     $header = false;
                 } elseif (strpos($line, ':')) {
                     list($key, $value) = explode(':', $line, 2);
                     $headersfound[trim($key)] = trim($value);
                     $lastheader = trim($key);
                 } else {
                     if ($lastheader != '') {
                         $headersfound[$lastheader] .= ' ' . trim($line);
                     } else {
                         // headers do not start with an ordinary header line?
                         // then assume no headers at all
                         $header = false;
                     }
                 }
             } else {
                 $body .= $line . $EOL;
             }
         }
         // now we build a new MimePart for the current Message Part:
         $newPart = new Zend_Mime_Part($body);
         foreach ($headersfound as $key => $value) {
             /**
              * @todo check for characterset and filename
              */
             switch ($key) {
                 case 'Content-Type':
                     $newPart->type = $value;
                     break;
                 case 'Content-Transfer-Encoding':
                     $newPart->encoding = $value;
                     break;
                 case 'Content-ID':
                     $newPart->id = trim($value, '<>');
                     break;
                 case 'Content-Disposition':
                     $newPart->disposition = $value;
                     break;
                 case 'Content-Description':
                     $newPart->description = $value;
                     break;
                 default:
                     throw new Zend_Exception('Unknown header ignored for MimePart:' . $key);
             }
         }
         $res->addPart($newPart);
     }
     return $res;
 }
 /**
  * Decodes a MIME encoded string and returns a Zend_Mime_Message object with
  * all the MIME parts set according to the given string
  *
  * @param string $message
  * @param string $boundary
  * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
  * @return Zend_Mime_Message
  */
 public static function createFromMessage($message, $boundary, $EOL = Zend_Mime::LINEEND)
 {
     require_once 'Zend/Mime/Decode.php';
     $parts = Zend_Mime_Decode::splitMessageStruct($message, $boundary, $EOL);
     $res = new Zend_Mime_Message();
     foreach ($parts as $part) {
         // now we build a new MimePart for the current Message Part:
         $newPart = new Zend_Mime_Part($part);
         foreach ($part['header'] as $key => $value) {
             /**
              * @todo check for characterset and filename
              */
             // list($key, $value) = $header;
             switch ($key) {
                 case 'content-type':
                     $newPart->type = $value;
                     break;
                 case 'content-transfer-encoding':
                     $newPart->encoding = $value;
                     break;
                 case 'content-id':
                     $newPart->id = trim($value, '<>');
                     break;
                 case 'Content-Disposition':
                     $newPart->disposition = $value;
                     break;
                 case 'content-description':
                     $newPart->description = $value;
                     break;
                 default:
                     throw new Zend_Exception('Unknown header ignored for MimePart:' . $key);
             }
         }
         $res->addPart($newPart);
     }
     return $res;
 }
 public function testCSVFileUpload()
 {
     $this->markTestIncomplete('The implementation is not yet finished and needs adaptation to new unit tests system.');
     try {
         $this->_httpClient->setUri($this->_baseUri . '/files');
         $this->_httpClient->setMethod(Zend_Http_Client::POST);
         $msg = new Zend_Mime_Message();
         $jsonPart = new Zend_Mime_Part('{}');
         $jsonPart->type = 'application/json';
         $jsonPart->filename = microtime(true) . '.json';
         $jsonPart->disposition = 'attachment';
         $msg->addPart($jsonPart);
         $xmlPart = new Zend_Mime_Part($this->_icc . ',GEM17223,2,SIM_Model,APN1,10.1.2.1,1,TM SPAIN');
         $xmlPart->type = 'text/plain';
         $xmlPart->filename = microtime(true) . '.csv';
         $xmlPart->disposition = 'attachment';
         $msg->addPart($xmlPart);
         $this->_httpClient->setHeaders('Content-Type', 'multipart/mixed; boundary="' . $msg->getMime()->boundary() . '"');
         $this->_httpClient->setRawData($msg->generateMessage());
         $res = $this->_httpClient->request();
         $this->assertEquals(200, $res->getStatus());
         $obj = json_decode($res->getBody());
         $this->assertTrue(is_object($obj));
         $this->assertNotEmpty($obj->customerData);
     } catch (Exception $e) {
         $this->fail($e->getMessage());
     }
 }