Skip to content

Commit

Permalink
Send attachment data to Ray
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Jul 11, 2024
1 parent d37e2be commit 3e19a01
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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()),
];
})->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
14 changes: 14 additions & 0 deletions tests/TestClasses/TestMailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@
namespace Spatie\LaravelRay\Tests\TestClasses;

use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;

class TestMailable extends Mailable
{
public function build()
{
return $this->markdown('mails.test');
}

/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [
Attachment::fromData(fn () => 'file1')->as('file_1.txt')->withMime('text/plain'),
Attachment::fromData(fn () => 'file2')->as('file_2.txt')->withMime('text/plain'),
];
}
}
1 change: 1 addition & 0 deletions tests/Unit/MailableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
->send(new TestMailable());

expect($this->client->sentRequests())->toHaveCount(1);
expect($this->client->sentPayloads()[0]['content']['attachments'])->toHaveCount(2);
});

it('can send multiple mailable payloads', function () {
Expand Down

0 comments on commit 3e19a01

Please sign in to comment.