Skip to content

Commit

Permalink
Parser->getInlineParts() method added, per php-mime-mail-parser#163
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianfm committed Oct 24, 2017
1 parent a3d5b16 commit 8c19e52
Show file tree
Hide file tree
Showing 3 changed files with 762 additions and 21 deletions.
56 changes: 40 additions & 16 deletions src/Parser.php
Expand Up @@ -359,22 +359,9 @@ public function getMessageBody($type = 'text')
]; ];


if (in_array($type, array_keys($mime_types))) { if (in_array($type, array_keys($mime_types))) {
foreach ($this->parts as $partId => $part) { $part_type = $type === 'htmlEmbedded' ? 'html' : $type;
if ($this->getPart('content-type', $part) == $mime_types[$type] $inline_parts = $this->getInlineParts($part_type);
&& $this->getPart('content-disposition', $part) != 'attachment' $body = empty($inline_parts) ? '' : $inline_parts[0];
&& !$this->partIdIsChildOfAnAttachment($partId)
) {
$headers = $this->getPart('headers', $part);
$encodingType = array_key_exists('content-transfer-encoding', $headers) ?
$headers['content-transfer-encoding'] : '';
if (is_array($encodingType)) {
$encodingType = $encodingType[0];
}
$body = $this->decodeContentTransfer($this->getPartBody($part), $encodingType);
$body = $this->charset->decodeCharset($body, $this->getPartCharset($part));
break;
}
}
} else { } else {
throw new Exception('Invalid type specified for getMessageBody(). "type" can either be text or html.'); throw new Exception('Invalid type specified for getMessageBody(). "type" can either be text or html.');
} }
Expand Down Expand Up @@ -430,6 +417,43 @@ public function getAddresses($name)
return mailparse_rfc822_parse_addresses($value); return mailparse_rfc822_parse_addresses($value);
} }


/**
* Returns the attachments contents in order of appearance
*
* @return Attachment[]
*/
public function getInlineParts($type = 'text')
{
$inline_parts = [];
$dispositions = ['inline'];
$mime_types = [
'text' => 'text/plain',
'html' => 'text/html',
];

if (!in_array($type, array_keys($mime_types))) {
throw new Exception('Invalid type specified for getInlineParts(). "type" can either be text or html.');
}

foreach ($this->parts as $partId => $part) {
if ($this->getPart('content-type', $part) == $mime_types[$type]
&& $this->getPart('content-disposition', $part) != 'attachment'
&& !$this->partIdIsChildOfAnAttachment($partId)
) {
$headers = $this->getPart('headers', $part);
$encodingType = array_key_exists('content-transfer-encoding', $headers) ?
$headers['content-transfer-encoding'] : '';
if (is_array($encodingType)) {
$encodingType = $encodingType[0];
}
$undecoded_body = $this->decodeContentTransfer($this->getPartBody($part), $encodingType);
$inline_parts[] = $this->charset->decodeCharset($undecoded_body, $this->getPartCharset($part));
}
}

return $inline_parts;
}

/** /**
* Returns the attachments contents in order of appearance * Returns the attachments contents in order of appearance
* *
Expand Down
28 changes: 23 additions & 5 deletions tests/ParserTest.php
Expand Up @@ -63,19 +63,37 @@ public function testInlineAttachmentsFalse(
$attachmentExpected[6], $attachmentExpected[6],
md5(serialize($attachments[$iterAttachments]->getHeaders())) md5(serialize($attachments[$iterAttachments]->getHeaders()))
); );

$iterAttachments++; $iterAttachments++;
} }
} }
} }


/**
* test for being able to extract multiple inline text/plain & text/html parts
* related to issue #163
*
* @return return type
*/
public function testMultiPartInline()
{
$file = __DIR__ .'/mails/issue163';
$Parser = new Parser();
$Parser->setText(file_get_contents($file));
$inline_parts = $Parser->getInlineParts('text');
$this->assertEquals(is_array($inline_parts), true);
$this->assertEquals(count($inline_parts), 2);
$this->assertEquals($inline_parts[0],"First we have a text block, then we insert an image:\r\n\r\n");
$this->assertEquals($inline_parts[1],"\r\n\r\nThen we have more text\r\n\r\n-- excuse brevity, sent from my phone.");
}

public function testIlligalAttachmentFilenameForDispositionFilename() public function testIlligalAttachmentFilenameForDispositionFilename()
{ {
$file = __DIR__ . '/mails/issue133'; $file = __DIR__ . '/mails/issue133';
$Parser = new Parser(); $Parser = new Parser();
$Parser->setText(file_get_contents($file)); $Parser->setText(file_get_contents($file));
$attachments = $Parser->getAttachments(false); $attachments = $Parser->getAttachments(false);

$this->assertEquals("attach_01", $attachments[0]->getFilename()); $this->assertEquals("attach_01", $attachments[0]->getFilename());
} }


Expand Down Expand Up @@ -126,15 +144,15 @@ public function testAttachmentsWithDuplicatesRandom()
// Default: generate random filename, so we should have two files // Default: generate random filename, so we should have two files
$this->assertEquals(2, count($attachmentFiles)); $this->assertEquals(2, count($attachmentFiles));
} }

public function testMultipleContentTransferEncodingHeader() public function testMultipleContentTransferEncodingHeader()
{ {
$file = __DIR__.'/mails/issue126'; $file = __DIR__.'/mails/issue126';
$Parser = new Parser(); $Parser = new Parser();
$Parser->setText(file_get_contents($file)); $Parser->setText(file_get_contents($file));
$Parser->getMessageBody('text'); $Parser->getMessageBody('text');
} }

public function testCreatingMoreThanOneInstanceOfParser() public function testCreatingMoreThanOneInstanceOfParser()
{ {
$file = __DIR__.'/mails/issue84'; $file = __DIR__.'/mails/issue84';
Expand Down Expand Up @@ -1618,7 +1636,7 @@ public function providerRFC822AttachmentsWithDifferentTextTypes()
200 character lower-limit in order to avoid preferring future HTML versions of the 200 character lower-limit in order to avoid preferring future HTML versions of the
body... filler filler filler filler filler filler filler filler filler.\n" body... filler filler filler filler filler filler filler filler filler.\n"
], ],
'Text-only message, with text-only RFC822 attachment, 'Text-only message, with text-only RFC822 attachment,
should have text body but not include attachment part' => [ should have text body but not include attachment part' => [
__DIR__.'/mails/issue158c', __DIR__.'/mails/issue158c',
'text', 'text',
Expand Down

0 comments on commit 8c19e52

Please sign in to comment.