Skip to content

Commit

Permalink
Fix removing text and html parts from apple mail
Browse files Browse the repository at this point in the history
  • Loading branch information
zbateson committed Mar 5, 2017
1 parent 1a3789f commit b27911e
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 181 deletions.
28 changes: 18 additions & 10 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@ protected function getContentPartByMimeType($mimeType)
return null;
}
$type = strtolower($this->contentPart->getHeaderValue('Content-Type', 'text/plain'));
if ($type === 'multipart/alternative') {
return $this->getPartByMimeType($mimeType);
} elseif ($type === $mimeType) {
if ($type === $mimeType) {
return $this->contentPart;
}
return null;
return $this->getPartByMimeType($mimeType);
}

/**
Expand All @@ -190,7 +188,7 @@ protected function getContentPartByMimeType($mimeType)
*/
private function overrideAlternativeMessageContentFromContentPart(MimePart $part)
{
$contentType = $part->getHeaderValue('Content-Type');
$contentType = $part->getHeader('Content-Type')->getRawValue();
if ($contentType === null) {
$contentType = 'text/plain; charset="us-ascii"';
}
Expand All @@ -204,6 +202,10 @@ private function overrideAlternativeMessageContentFromContentPart(MimePart $part
);
$this->attachContentResourceHandle($part->getContentResourceHandle());
$part->detachContentResourceHandle();
foreach ($part->getChildParts() as $child) {
$child->setParent($this);
parent::addPart($child);
}
$this->removePart($part);
}

Expand All @@ -217,12 +219,15 @@ private function overrideAlternativeMessageContentFromContentPart(MimePart $part
*/
private function removePartFromAlternativeContentPart(MimePart $part)
{
foreach ($part->getAllParts() as $cpart) {
$this->removePart($cpart);
}
$this->removePart($part);
$contentPart = $this->contentPart->getPart(0);
if ($contentPart !== null) {
if ($this->contentPart === $this) {
$this->overrideAlternativeMessageContentFromContentPart($contentPart);
} elseif ($this->contentPart->getPartCount() === 1) {
} elseif ($this->contentPart->getChildCount() === 1) {
$this->removePart($this->contentPart);
$contentPart->setParent($this);
$this->contentPart = null;
Expand All @@ -241,11 +246,14 @@ private function removePartFromAlternativeContentPart(MimePart $part)
*/
private function removeContentPartFromAlternative($contentType)
{
$parts = $this->contentPart->getAllParts();
foreach ($parts as $part) {
$type = $part->getHeaderValue('Content-Type', 'text/plain');
$parts = $this->contentPart->getChildParts();
foreach ($parts as $child) {
$type = $child->getHeaderValue('Content-Type', 'text/plain');
if (strcasecmp($type, $contentType) === 0) {
$this->removePartFromAlternativeContentPart($part);
$this->removePartFromAlternativeContentPart($child);
return true;
} elseif ($child->isMultiPart() && $child->getPartByMimeType($contentType)) {
$this->removePartFromAlternativeContentPart($child);
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Message/MimePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function getChildCount()
public function getPartByMimeType($mimeType)
{
$key = strtolower($mimeType);
if (isset($this->mimeToPart[$key])) {
if (!empty($this->mimeToPart[$key])) {
return $this->mimeToPart[$key][0];
}
return null;
Expand Down
Loading

0 comments on commit b27911e

Please sign in to comment.