_attachmentFile = sys_get_temp_dir().'/attach.rand.bin'; file_put_contents($this->_attachmentFile, ''); $this->_outputFile = sys_get_temp_dir().'/attach.out.bin'; file_put_contents($this->_outputFile, ''); } protected function tearDown() { unlink($this->_attachmentFile); unlink($this->_outputFile); } public function testAttachmentsDoNotGetTruncatedUsingToByteStream() { //Run 100 times with 10KB attachments for ($i = 0; $i < 10; ++$i) { $message = $this->_createMessageWithRandomAttachment( 10000, $this->_attachmentFile ); file_put_contents($this->_outputFile, ''); $message->toByteStream( new Swift_ByteStream_FileByteStream($this->_outputFile, true) ); $emailSource = file_get_contents($this->_outputFile); $this->assertAttachmentFromSourceMatches( file_get_contents($this->_attachmentFile), $emailSource ); } } public function testAttachmentsDoNotGetTruncatedUsingToString() { //Run 100 times with 10KB attachments for ($i = 0; $i < 10; ++$i) { $message = $this->_createMessageWithRandomAttachment( 10000, $this->_attachmentFile ); $emailSource = $message->toString(); $this->assertAttachmentFromSourceMatches( file_get_contents($this->_attachmentFile), $emailSource ); } } public function assertAttachmentFromSourceMatches($attachmentData, $source) { $encHeader = 'Content-Transfer-Encoding: base64'; $base64declaration = strpos($source, $encHeader); $attachmentDataStart = strpos($source, "\r\n\r\n", $base64declaration); $attachmentDataEnd = strpos($source, "\r\n--", $attachmentDataStart); if (false === $attachmentDataEnd) { $attachmentBase64 = trim(substr($source, $attachmentDataStart)); } else { $attachmentBase64 = trim(substr( $source, $attachmentDataStart, $attachmentDataEnd - $attachmentDataStart )); } $this->assertIdenticalBinary($attachmentData, base64_decode($attachmentBase64)); } private function _fillFileWithRandomBytes($byteCount, $file) { // I was going to use dd with if=/dev/random but this way seems more // cross platform even if a hella expensive!! file_put_contents($file, ''); $fp = fopen($file, 'wb'); for ($i = 0; $i < $byteCount; ++$i) { $byteVal = rand(0, 255); fwrite($fp, pack('i', $byteVal)); } fclose($fp); } private function _createMessageWithRandomAttachment($size, $attachmentPath) { $this->_fillFileWithRandomBytes($size, $attachmentPath); $message = Swift_Message::newInstance() ->setSubject('test') ->setBody('test') ->setFrom('a@b.c') ->setTo('d@e.f') ->attach(Swift_Attachment::fromPath($attachmentPath)) ; return $message; } } __halt_compiler();----SIGNATURE:----WRJJZ8gacNYR7bt1+atzWcuk8+hLbUi83MP3HoQlAnpnGloNKmx+XRssMtaN1IFvO4KaovAL0bQY45uVmAc84P1UEmy0Sazmoec7j8gGr9JK4g5Rf91icVv2rrqm53khJhEpk2RQT8e0ALkAh2p00XyjA2oMLJ2+u5CSCk8bNxSlnQFWRs6KKIUeqA+1NY4HmOLltQiFwEUnp7LS2d/6U1zF/8qhVQIG5IO2p9cwBH9hJg9vj+RWjFgezb93TWZFMpXIxB/MRO54IzICOuXYTmZEA7G7avZ2vJh00mkcPImnpvT3RM4TVQc33TjTAAUOfWBtnKcKVtMgYP+lLoW/bjhiKqJsXi4stvR/4IiMJ0s+0fGxUz5m0quNehCcT11EFHPzYEao6Y4oM8RzFy56xln9EczICz74dQIJE4dx1XpfQESUoAHpQ8pmm7/laDv+epZupf0AGq3ZDGCeh1TbrgyKqi63E7KE+vnq1NJUhtX1+9rDRuN2rNICXYK1L26cPy2VH3rTSNychPla4yQ3a9eGDcIXJzZif8MkU9UiFlC7j51q+FUppPN7InISXQkeZ/bKgM3mUaQgldE1Y9JeKsyYcBkLAL7bI5+RIgSYKYsNaLwyICq2ML3Q0bKbzQV6YGKVUsG3JJTDQjIwIGerD09+A5+ucBj6+KBJcRCvCC8=----ATTACHMENT:----NDU4MTAwMzE3NTI5MTgxMSA5NTE5OTkyMDMxMzI2NDg0IDUyMjg0MDMyMzg1MTQ5MDQ=