diff --git a/src/Message.php b/src/Message.php index e802153..197668a 100644 --- a/src/Message.php +++ b/src/Message.php @@ -222,8 +222,9 @@ public static function createFromMessage($message, $boundary, $EOL = Mime::LINEE $res = new static(); foreach ($parts as $part) { + // now we build a new MimePart for the current Message Part: - $newPart = new Part($part['body']); + $properties = array(); foreach ($part['header'] as $header) { /** @var \Zend\Mail\Header\HeaderInterface $header */ /** @@ -234,30 +235,48 @@ public static function createFromMessage($message, $boundary, $EOL = Mime::LINEE $fieldValue = $header->getFieldValue(); switch (strtolower($fieldName)) { case 'content-type': - $newPart->type = $fieldValue; + $properties['type'] = $fieldValue; break; case 'content-transfer-encoding': - $newPart->encoding = $fieldValue; + $properties['encoding'] = $fieldValue; break; case 'content-id': - $newPart->id = trim($fieldValue,'<>'); + $properties['id'] = trim($fieldValue,'<>'); break; case 'content-disposition': - $newPart->disposition = $fieldValue; + $properties['disposition'] = $fieldValue; break; case 'content-description': - $newPart->description = $fieldValue; + $properties['description'] = $fieldValue; break; case 'content-location': - $newPart->location = $fieldValue; + $properties['location'] = $fieldValue; break; case 'content-language': - $newPart->language = $fieldValue; + $properties['language'] = $fieldValue; break; default: throw new Exception\RuntimeException('Unknown header ignored for MimePart:' . $fieldName); } } + + $body = $part['body']; + + if (isset($properties['encoding'])) { + switch ($properties['encoding']) { + case 'quoted-printable': + $body = quoted_printable_decode($body); + break; + case 'base64': + $body = base64_decode($body); + break; + } + } + + $newPart = new Part($body); + foreach ($properties as $key => $value) { + $newPart->$key = $value; + } $res->addPart($newPart); } diff --git a/test/MimeTest.php b/test/MimeTest.php index a108cec..1b3d94c 100644 --- a/test/MimeTest.php +++ b/test/MimeTest.php @@ -135,6 +135,33 @@ public static function dataTestEncodeMailHeaderBase64() ); } + public static function dataTestFromMessageDecode() + { + return array( + array('äöü', 'quoted-printable', '=C3=A4=C3=B6=C3=BC'), + array('Alle meine Entchen schwimmen in dem See, schwimmen in dem See, Köpfchen in das Wasser, Schwänzchen in die Höh!', 'quoted-printable', 'Alle meine Entchen schwimmen in dem See, schwimmen in dem See, K=C3=B6pfche= +n in das Wasser, Schw=C3=A4nzchen in die H=C3=B6h!'), + array('foobar', 'base64', 'Zm9vYmFyCg=='), + ); + } + + /** + * @dataProvider dataTestFromMessageDecode + */ + public function testFromMessageDecode($input, $encoding, $result) + { + $parts = Mime\Message::createFromMessage( + '--089e0141a1902f83ee04e0a07b7a'."\r\n" + .'Content-Type: text/plain; charset=UTF-8'."\r\n" + .'Content-Transfer-Encoding: '.$encoding."\r\n" + ."\r\n" + .$result."\r\n" + .'--089e0141a1902f83ee04e0a07b7a--', + '089e0141a1902f83ee04e0a07b7a' + )->getParts(); + $this->assertSame($input."\n", $parts[0]->getRawContent()); + } + /** * @group ZF-1688 */