Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send attachment data to Ray #357

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Payloads/LoggedMailPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ZBateson\MailMimeParser\Header\Part\AddressPart;
use ZBateson\MailMimeParser\IMessage;
use ZBateson\MailMimeParser\MailMimeParser;
use ZBateson\MailMimeParser\Message\MimePart;

class LoggedMailPayload extends Payload
{
Expand All @@ -29,6 +30,9 @@ class LoggedMailPayload extends Payload
/** @var array */
protected $bcc;

/** @var array */
protected $attachments;

public static function forLoggedMail(string $loggedMail): self
{
$parser = new MailMimeParser();
Expand All @@ -38,6 +42,7 @@ public static function forLoggedMail(string $loggedMail): self
// get the part in $loggedMail that starts with <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0

$content = self::getMailContent($loggedMail, $message);
$attachments = self::getMailAttachments($message);

return new self(
$content,
Expand All @@ -46,6 +51,7 @@ public static function forLoggedMail(string $loggedMail): self
self::convertHeaderToPersons($message->getHeader(HeaderConsts::TO)),
self::convertHeaderToPersons($message->getHeader(HeaderConsts::CC)),
self::convertHeaderToPersons($message->getHeader(HeaderConsts::BCC)),
$attachments,
);
}

Expand All @@ -55,14 +61,16 @@ public function __construct(
?string $subject = null,
array $to = [],
array $cc = [],
array $bcc = []
array $bcc = [],
array $attachments = []
) {
$this->html = $html;
$this->from = $from;
$this->subject = $subject;
$this->to = $to;
$this->cc = $cc;
$this->bcc = $bcc;
$this->attachments = $attachments;
}

protected static function getMailContent(string $loggedMail, IMessage $message): string
Expand All @@ -76,6 +84,19 @@ protected static function getMailContent(string $loggedMail, IMessage $message):
return substr($loggedMail, $startOfHtml) ?? '';
}

protected static function getMailAttachments(IMessage $message): array
{
return collect($message->getAllAttachmentParts())
->map(function (MimePart $attachmentPart) {
return [
'filename' => $attachmentPart->getFilename(),
'content_id' => $attachmentPart->getContentId(),
'content_type' => $attachmentPart->getContentType(),
'content' => base64_encode($attachmentPart->getContent()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth checking the size of the content before doing this? Ray can sometimes struggle a bit with large payloads in my experience.

];
})->toArray();
}

public function getType(): string
{
return 'mailable';
Expand All @@ -90,6 +111,7 @@ public function getContent(): array
'to' => $this->to,
'cc' => $this->cc,
'bcc' => $this->bcc,
'attachments' => $this->attachments,
];
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Payloads/LoggedMailPayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
'email' => '[email protected]',
],
],
'attachments' => [],
])->toEqual($payload->getContent());
});

Expand Down Expand Up @@ -85,5 +86,6 @@
],
'cc' => [],
'bcc' => [],
'attachments' => [],
])->toEqual($payload->getContent());
});
4 changes: 3 additions & 1 deletion tests/TestClasses/TestMailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class TestMailable extends Mailable
{
public function build()
{
return $this->markdown('mails.test');
return $this->markdown('mails.test')
->attachData('file1', 'file_1.txt')
->attachData('file2', 'file_2.txt');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to test attaching binary files?

}
}
Loading