Permalink
Browse files

Parser->getInlineParts() method added, per php-mime-mail-parser#163

  • Loading branch information...
dorianfm committed Oct 24, 2017
1 parent a3d5b16 commit 8c19e525e654530df9612632e0e4c2464c9b8e64
Showing with 762 additions and 21 deletions.
  1. +40 −16 src/Parser.php
  2. +23 −5 tests/ParserTest.php
  3. +699 −0 tests/mails/issue163
@@ -359,22 +359,9 @@ public function getMessageBody($type = 'text')
];
if (in_array($type, array_keys($mime_types))) {
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];
}
$body = $this->decodeContentTransfer($this->getPartBody($part), $encodingType);
$body = $this->charset->decodeCharset($body, $this->getPartCharset($part));
break;
}
}
$part_type = $type === 'htmlEmbedded' ? 'html' : $type;
$inline_parts = $this->getInlineParts($part_type);
$body = empty($inline_parts) ? '' : $inline_parts[0];
} else {
throw new Exception('Invalid type specified for getMessageBody(). "type" can either be text or html.');
}
@@ -430,6 +417,43 @@ public function getAddresses($name)
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
*
@@ -63,19 +63,37 @@ public function testInlineAttachmentsFalse(
$attachmentExpected[6],
md5(serialize($attachments[$iterAttachments]->getHeaders()))
);
$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()
{
$file = __DIR__ . '/mails/issue133';
$Parser = new Parser();
$Parser->setText(file_get_contents($file));
$attachments = $Parser->getAttachments(false);
$this->assertEquals("attach_01", $attachments[0]->getFilename());
}
@@ -126,15 +144,15 @@ public function testAttachmentsWithDuplicatesRandom()
// Default: generate random filename, so we should have two files
$this->assertEquals(2, count($attachmentFiles));
}
public function testMultipleContentTransferEncodingHeader()
{
$file = __DIR__.'/mails/issue126';
$Parser = new Parser();
$Parser->setText(file_get_contents($file));
$Parser->getMessageBody('text');
}
public function testCreatingMoreThanOneInstanceOfParser()
{
$file = __DIR__.'/mails/issue84';
@@ -1618,7 +1636,7 @@ public function providerRFC822AttachmentsWithDifferentTextTypes()
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"
],
'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' => [
__DIR__.'/mails/issue158c',
'text',
Oops, something went wrong.

0 comments on commit 8c19e52

Please sign in to comment.