_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:----NqhsQejt8cuZHcIrSEjjjJsTCKzj7k63lZqIGZMwxZiPJcfPOhV52HlR/NsmG2mrQIzbcekbsAJgfK5wHgXey7Pds8x16DZ2Zo46vzGYBaDybJt0c5VKqelOdOF3iudnlY1jpTxpeJSZ+yQuGOd2RCqPG4T3fgo37hvsUsy+4fDHcmMEm1JZ+23glK+8kugEfLSGLnmiFYluRaN72dtMb4WbAMGrim/DXeI28KO5IdCyWtPLe6O5woLyXa+rXhNxdQM/VigX8juMRikneK5Gfzyg8JvWfQUbBcpO08mIQ7Y8HNwK7ZUJBVBnbipsfgl7WJIrtBeimf70mGWnRiqlNn718yT2HfemrXLQEpdY5TGKyVl1PoMCjvMVLj4uUgfw3YjNLNTj+20z/LEp5/Jh8twUEou4IPRDOHUjrBO7MB6XgXi7QLmYtwVzRsdSh5KNnRvl5026bfX8dpe+OZLp2JbfGdp877H020Rw2W8OuN7knLyY57L5xCfBhSSKthV7Ct87UNd2TZfD2RaLmexXpJP5laoVRzafSBNjprK3WM2mAZb0hyEoLhvkklMYFf51J8+MXUvSTcLe2kLvMjISk8a7NDRQacN3iSirNbShkPHVuUic/uiPbNPZD8AqDkC1wcO+nZvlPg6+DwNgjem/KvB1Yu0/aE6P2sJDl1EIPGk=----ATTACHMENT:----MTc3NjU1MzQyNTM3MTI2OSA4NDgzMTM3MzgwMDU1MDc2IDI3NTMxNzAyOTYwMzE1Njc=