Skip to content

Commit

Permalink
add a few helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
arukompas committed Aug 25, 2023
1 parent f996b56 commit 25acddc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,36 @@ public function getParts(): array
return $this->parts;
}

public function getHtmlPart(): ?MessagePart
{
foreach ($this->parts as $part) {
if ($part->isHtml()) {
return $part;
}
}

return null;
}

public function getTextPart(): ?MessagePart
{
foreach ($this->parts as $part) {
if ($part->isText()) {
return $part;
}
}

return null;
}

/**
* @return MessagePart[]
*/
public function getAttachments(): array
{
return array_filter($this->parts, fn ($part) => $part->isAttachment());
}

protected function parse()
{
// Parse the email message into headers and body
Expand Down
15 changes: 15 additions & 0 deletions src/MessagePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ public function getContent(): string
return $this->content;
}

public function isHtml(): bool
{
return str_starts_with(strtolower($this->getContentType()), 'text/html');
}

public function isText(): bool
{
return str_starts_with(strtolower($this->getContentType()), 'text/plain');
}

public function isImage(): bool
{
return str_starts_with(strtolower($this->getContentType()), 'image/');
}

public function isAttachment(): bool
{
return str_starts_with($this->getHeader('Content-Disposition'), 'attachment');
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@

expect($parts)->toHaveCount(2);

$htmlPart = $parts[0];

expect($htmlPart->getContentType())->toBe('text/html; charset="utf-8"')
->and($htmlPart->isHtml())->toBe(true);

$attachmentPart = $parts[1];

expect($attachmentPart->getContent())->toBe('This is a test string')
Expand Down

0 comments on commit 25acddc

Please sign in to comment.