Skip to content

Commit

Permalink
Don't lose the multipart message when setting trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
jonb-sf committed Mar 31, 2022
1 parent 629a44f commit d4b4664
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/MailTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,30 +186,35 @@ protected function createTrackers($message)
$subject = $message->getSubject();

$original_content = $message->getBody();
$original_html = '';
if(get_class($original_content) == AlternativePart::class) {
$messageBody = $message->getBody() ?: [];
$newParts = [];
foreach($messageBody->getParts() as $part) {
if($part->getMediaSubtype() == 'html') {
$original_html = $part->getBody();
$newParts[] = new TextPart(
$this->addTrackers($original_html, $hash),
$message->getHtmlCharset(),
$part->getMediaSubtype(),
null
);
} else {
$newParts[] = $part;
}
}
$message->setBody(new AlternativePart(...$newParts));
} else {
$original_html = $original_content->getBody();
}

$mime = $original_content->getMediaType().'/'.$original_content->getMediaSubtype();

if ($mime === 'text/html' ||
($mime === 'multipart/alternative' && $original_content) ||
($mime === 'multipart/mixed' && $original_content)
) {
$message->setBody(new TextPart(
$this->addTrackers($original_html, $hash),
$message->getHtmlCharset(),
$original_content->getMediaSubtype(),
null
)
);
if($original_content->getMediaSubtype() == 'html') {
$message->setBody(new TextPart(
$this->addTrackers($original_html, $hash),
$message->getHtmlCharset(),
$original_content->getMediaSubtype(),
null
)
);
}
}

$tracker = SentEmail::create([
Expand Down

0 comments on commit d4b4664

Please sign in to comment.