diff --git a/src/Payloads/LoggedMailPayload.php b/src/Payloads/LoggedMailPayload.php index 8ad50e4..18076c2 100644 --- a/src/Payloads/LoggedMailPayload.php +++ b/src/Payloads/LoggedMailPayload.php @@ -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 { @@ -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(); @@ -38,6 +42,7 @@ public static function forLoggedMail(string $loggedMail): self // get the part in $loggedMail that starts with getHeader(HeaderConsts::TO)), self::convertHeaderToPersons($message->getHeader(HeaderConsts::CC)), self::convertHeaderToPersons($message->getHeader(HeaderConsts::BCC)), + $attachments, ); } @@ -55,7 +61,8 @@ public function __construct( ?string $subject = null, array $to = [], array $cc = [], - array $bcc = [] + array $bcc = [], + array $attachments = [] ) { $this->html = $html; $this->from = $from; @@ -63,6 +70,7 @@ public function __construct( $this->to = $to; $this->cc = $cc; $this->bcc = $bcc; + $this->attachments = $attachments; } protected static function getMailContent(string $loggedMail, IMessage $message): string @@ -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'; @@ -90,6 +111,7 @@ public function getContent(): array 'to' => $this->to, 'cc' => $this->cc, 'bcc' => $this->bcc, + 'attachments' => $this->attachments, ]; } diff --git a/tests/Payloads/LoggedMailPayloadTest.php b/tests/Payloads/LoggedMailPayloadTest.php index bae1d4a..9f9a2df 100644 --- a/tests/Payloads/LoggedMailPayloadTest.php +++ b/tests/Payloads/LoggedMailPayloadTest.php @@ -54,6 +54,7 @@ 'email' => 'willem@spatie.be', ], ], + 'attachments' => [], ])->toEqual($payload->getContent()); }); @@ -85,5 +86,6 @@ ], 'cc' => [], 'bcc' => [], + 'attachments' => [], ])->toEqual($payload->getContent()); }); diff --git a/tests/TestClasses/TestMailable.php b/tests/TestClasses/TestMailable.php index df2fc5a..8281324 100644 --- a/tests/TestClasses/TestMailable.php +++ b/tests/TestClasses/TestMailable.php @@ -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'); } }