Skip to content

Commit

Permalink
Merge pull request #166 from theusefularts/master
Browse files Browse the repository at this point in the history
Changes per #163
  • Loading branch information
eXorus committed Nov 2, 2017
2 parents 0fe5834 + a3f3a0e commit c6884c7
Show file tree
Hide file tree
Showing 4 changed files with 777 additions and 23 deletions.
60 changes: 43 additions & 17 deletions src/Parser.php
Expand Up @@ -359,24 +359,13 @@ 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.');
throw new Exception(
'Invalid type specified for getMessageBody(). Expected: text, html or htmlEmbeded.'
);
}

if ($type == 'htmlEmbedded') {
Expand Down Expand Up @@ -430,6 +419,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
*
Expand Down
13 changes: 12 additions & 1 deletion tests/ExceptionTest.php
Expand Up @@ -83,14 +83,25 @@ public function testGetHeadersRaw()

/**
* @expectedException Exception
* @expectedExceptionMessage Invalid type specified for getMessageBody(). "type" can either be text or html.
* @expectedExceptionMessage Invalid type specified for getMessageBody(). Expected: text, html or htmlEmbeded.
*/
public function testgetMessageBody()
{
$Parser = new Parser();
$Parser->getMessageBody('azerty');
}

/**
* @expectedException Exception
* @expectedExceptionMessage Invalid type specified for getInlineParts(). "type" can either be text or html.
*/
public function testgetInlineParts()
{
$Parser = new Parser();
$Parser->getInlineParts('azerty');
}


/**
* @expectedException Exception
* @expectedExceptionMessage You must not call MimeMailParser::setText with an empty string parameter
Expand Down
28 changes: 23 additions & 5 deletions tests/ParserTest.php
Expand Up @@ -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-- 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());
}

Expand Down Expand Up @@ -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';
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
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',
Expand Down

0 comments on commit c6884c7

Please sign in to comment.