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

Mime\Message: createFromString: decode transfer encoding #4934

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions library/Zend/Mime/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 tests/ZendTest/Mime/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