Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/**
Expand All @@ -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);
}

Expand Down
27 changes: 27 additions & 0 deletions test/MimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 178b782

Please sign in to comment.