beyondcode / laravel-mailbox

Catch incoming emails in your Laravel application
https://beyondco.de/docs/laravel-mailbox/getting-started/introduction
MIT License
1.04k stars 127 forks source link

How can we store attachments using Spatie Medialibrary? #98

Closed skakrecha closed 2 years ago

skakrecha commented 2 years ago

Hi , i am using the package to store incoming mails in db, but want to use spatie medialibrary to store the attachments.

Thanks.

Yiddishe-Kop commented 2 years ago

Here is a simple approach:

 // save incoming attachments
collect($inboundEmail->attachments())->each(function (MessagePart $attachment) use ($model) {

    $model->addMediaFromString($attachment->getContent())
          ->usingFileName($attachment->getFilename() ?? 'unnamed')
          ->toMediaCollection('files');

});