Skip to content

Commit

Permalink
Merge pull request #39 from shaffe-fr/feat/raw-message
Browse files Browse the repository at this point in the history
Prevent raw messages to be processed
  • Loading branch information
eduardokum authored Apr 20, 2021
2 parents e5d0a42 + 94f47e4 commit 06518c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Listeners/SwiftEmbedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,35 @@ private function attachImages()
return;
}

// Invalid HTML (raw message)
if ($this->shouldSkipDocument($document)) {
return;
}

// Add images
$this->attachImagesToDom($document);

// Replace body
$this->message->setBody($parser->saveHTML($document));
}

/**
* @param DOMDocument $document
* @return bool
*/
private function shouldSkipDocument($document)
{
if ($document->childNodes->count() != 1) {
return false;
}

if ($document->childNodes->item(0)->nodeType == XML_DOCUMENT_TYPE_NODE) {
return true;
}

return false;
}

/**
* @param DOMDocument $document
* @return string
Expand Down
13 changes: 13 additions & 0 deletions tests/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,17 @@ public function testDoesntHandleSendPerformedEvent()
$embedPlugin->sendPerformed($this->createSwiftEvent($message))
);
}

/**
* @test
*/
public function testDoesntTransformRawMessages()
{
$message = $this->handleBeforeSendPerformedEvent('raw-message.txt', [
'enabled' => true,
'method' => 'attachment',
]);

$this->assertEquals($this->getLibraryFile('raw-message.txt'), $message->getBody());
}
}
2 changes: 2 additions & 0 deletions tests/lib/raw-message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The is a raw message that should be skipped.
It doesn't contain any images.

0 comments on commit 06518c1

Please sign in to comment.