diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 4474d42..d094a12 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -4,17 +4,15 @@ on: [push, pull_request] jobs: test: - runs-on: ubuntu-16.04 + runs-on: ubuntu-latest strategy: fail-fast: true matrix: - php: [ 7.3, 8.0 ] - laravel: [ 7.*, 8.* ] + php: [ 8.0, 8.1 ] + laravel: [ 9.* ] include: - - laravel: 7.* - testbench: 5.* - - laravel: 8.* - testbench: 6.* + - laravel: 9.* + testbench: 7.* name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} diff --git a/.gitignore b/.gitignore index 660fc15..b3a516b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +/.idea +/.vscode /vendor composer.lock /phpunit.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 32f5ca7..3cfdb42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,41 @@ All notable changes to `laravel-mail-export` will be documented in this file. +## 2.0.0 - 2022-08-04 + +- Laravel 9 Support **Breaking change** [#17](https://github.com/Pod-Point/laravel-mail-export/pull/17) +- Moved from `swiftmailer/swiftmailer` to `symfony/mailer` +- Thank you to [fridzema](https://github.com/fridzema) for contributing [#14](https://github.com/Pod-Point/laravel-mail-export/pull/14) + +## 1.0.0 - 2022-08-04 + +- Minor improvements + bump up to `1.x` [#18](https://github.com/Pod-Point/laravel-mail-export/pull/18) +- New contributor [MatusBoa](https://github.com/MatusBoa) see [#15](https://github.com/Pod-Point/laravel-mail-export/pull/15) + +**Full Changelog**: https://github.com/Pod-Point/laravel-mail-export/compare/0.2.2...1.0.0 + +## 0.2.2 - 2021-09-14 + +- Bug: issue [#7](https://github.com/Pod-Point/laravel-mail-export/issues/7) Fix filesystems config file typo +- Enhancement: Add `MAIL_EXPORT` environment variable support + +## 0.2.1 - 2021-04-01 + +- Specify proper `.eml` mime type when saving the file into storage + +## 0.2.0 - 2021-04-01 + +- Support for PHP 8.x, Laravel 7.x and 8.x + +## 0.1.2 - 2021-09-14 + +- Bug: issue [#7](https://github.com/Pod-Point/laravel-mail-export/issues/7) Fix filesystems config file typo +- Enhancement: Add `MAIL_EXPORT` environment variable support + +## 0.1.1 - 2021-04-01 + +- Specify proper `.eml` mime type when saving the file into storage + ## 0.1.0 - 2021-03-29 -- first release supporting PHP 7.x & Laravel 5.x and 6.x +- First release supporting PHP 7.x, Laravel 5.x and 6.x diff --git a/README.md b/README.md index 28fe798..f65fa69 100644 --- a/README.md +++ b/README.md @@ -13,16 +13,22 @@ This can be useful when wanting to store emails sent for archive purposes. You can install the package via composer: -For Laravel 5.x and 6.x +For Laravel 9.x ```bash -composer require pod-point/laravel-mail-export:^0.1 +composer require pod-point/laravel-mail-export ``` For Laravel 7.x and 8.x ```bash -composer require pod-point/laravel-mail-export:^0.2 +composer require pod-point/laravel-mail-export:^1.0 +``` + +For Laravel 5.x and 6.x + +```bash +composer require pod-point/laravel-mail-export:^0.1 ``` ### Publishing the config file @@ -35,9 +41,9 @@ php artisan vendor:publish --provider="PodPoint\MailExport\MailExportServiceProv You will be able to specify: -* `enabled`: wether this package is enabled or not. +* `enabled`: whether this package is enabled or not. Once installed, it's enabled by default but the `MAIL_EXPORT` environment variable can be used to configure this. * `disk`: which disk to use by default. `null` will use the default disk from your application filesystem. -* `path`: the default path you would like to export your mails within a storage disk. +* `path`: the default path, within the configured disk, where mail will be exported. See our [`config/mail-export.php`](config/mail-export.php) for more details. @@ -57,7 +63,7 @@ use PodPoint\MailExport\Contracts\ShouldExport; class OrderShipped extends Mailable implements ShouldExport { use Exportable; - + // ... } ``` @@ -84,13 +90,13 @@ use PodPoint\MailExport\Contracts\ShouldExport; class OrderShipped extends Mailable implements ShouldExport { use Exportable; - + public $exportDisk = 'some_disk'; public $exportPath = 'some_path'; public $exportFilename = 'some_filename'; - + // ... } ``` @@ -109,9 +115,9 @@ use PodPoint\MailExport\Contracts\ShouldExport; class OrderShipped extends Mailable implements ShouldExport { use Exportable; - + // ... - + public function exportDisk(): string { return 'some_disk'; @@ -148,11 +154,11 @@ use Illuminate\Notifications\Notification; class OrderShipped extends Notification { // ... - + public function toMail($notifiable) { return (new Mailable($this->order))->to($notifiable->email); - } + } } ``` @@ -175,7 +181,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details. ## Credits - [themsaid](https://github.com/themsaid) and Spatie's [laravel-mail-preview](https://github.com/spatie/laravel-mail-preview) for some inspiration -- [Laravel Package Development](https://laravelpackage.com) documentation by [John Braun](https://github.com/Jhnbrn90) +- [Laravel Package Development](https://laravelpackage.com) documentation by [John Braun](https://github.com/Jhnbrn90) - [Pod Point](https://github.com/pod-point) - [All Contributors](https://github.com/pod-point/laravel-mail-export/graphs/contributors) @@ -189,4 +195,4 @@ The MIT License (MIT). Please see [License File](LICENCE.md) for more informatio Travel shouldn't damage the earth 🌍 -Made with ❤️ at [Pod Point](https://pod-point.com) +Made with ❤️  at [Pod Point](https://pod-point.com) diff --git a/composer.json b/composer.json index 748cda2..5c6c1b4 100644 --- a/composer.json +++ b/composer.json @@ -11,14 +11,14 @@ } ], "require": { - "php": "^7.2.5|^8.0", - "nesbot/carbon": "^2.0", - "illuminate/support": "~7.0|^8.0", - "illuminate/mail": "~7.0|^8.0", - "illuminate/filesystem": "~7.0|^8.0" + "php": "^8.0", + "illuminate/filesystem": "^9.0", + "illuminate/mail": "^9.0", + "illuminate/support": "^9.0", + "nesbot/carbon": "^2.0" }, "require-dev": { - "orchestra/testbench": "^5.0|^6.0" + "orchestra/testbench": "^7.0" }, "autoload": { "psr-4": { @@ -31,9 +31,6 @@ } }, "extra": { - "branch-alias": { - "dev-master": "0.2-dev" - }, "laravel": { "providers": [ "PodPoint\\MailExport\\MailExportServiceProvider" diff --git a/config/mail-export.php b/config/mail-export.php index f2e7619..eaabc0d 100644 --- a/config/mail-export.php +++ b/config/mail-export.php @@ -13,7 +13,7 @@ | */ - 'enabled' => true, + 'enabled' => env('MAIL_EXPORT', true), /* |-------------------------------------------------------------------------- diff --git a/src/Concerns/Exportable.php b/src/Concerns/Exportable.php index 76acf8f..ab21622 100644 --- a/src/Concerns/Exportable.php +++ b/src/Concerns/Exportable.php @@ -16,7 +16,7 @@ trait Exportable */ public function send($mailer) { - $this->withSwiftMessage(function ($message) { + $this->withSymfonyMessage(function ($message) { if (! $this instanceof ShouldExport) { return; } diff --git a/src/Events/MessageStored.php b/src/Events/MessageStored.php index a61e25e..68eac13 100644 --- a/src/Events/MessageStored.php +++ b/src/Events/MessageStored.php @@ -4,15 +4,16 @@ use Illuminate\Foundation\Events\Dispatchable; use PodPoint\MailExport\StorageOptions; +use Symfony\Component\Mime\Email; class MessageStored { use Dispatchable; /** - * The Swift message instance. + * The message instance. * - * @var \Swift_Message + * @var \Symfony\Component\Mime\Email */ public $message; @@ -20,18 +21,18 @@ class MessageStored * The filesystem storage options used to store the message including * the disk, the path and the filename with its extension. * - * @var StorageOptions + * @var \PodPoint\MailExport\StorageOptions */ public $storageOptions; /** * Create a new event instance. * - * @param \Swift_Message $message + * @param Email $message * @param StorageOptions $storageOptions * @return void */ - public function __construct($message, $storageOptions) + public function __construct(Email $message, StorageOptions $storageOptions) { $this->message = $message; $this->storageOptions = $storageOptions; diff --git a/src/Listeners/ExportMessage.php b/src/Listeners/ExportMessage.php index 26a52a8..d070ba2 100644 --- a/src/Listeners/ExportMessage.php +++ b/src/Listeners/ExportMessage.php @@ -6,6 +6,7 @@ use Illuminate\Mail\Events\MessageSent; use PodPoint\MailExport\Events\MessageStored; use PodPoint\MailExport\StorageOptions; +use Symfony\Component\Mime\Email; class ExportMessage { @@ -14,11 +15,6 @@ class ExportMessage */ protected $filesystem; - /** - * @var \Swift_Message - */ - protected $message; - /** * Create a new listener instance. * @@ -36,41 +32,39 @@ public function __construct(Factory $filesystem) */ public function handle(MessageSent $event) { - $this->message = $event->message; - - if ($this->shouldStoreMessage()) { - $this->storeMessage(); + if ($this->shouldStoreMessage($event->message)) { + $this->storeMessage($event->message); } } /** - * Finds out if wether we should store the mail or not. + * Finds out if whether we should store the mail or not. * * @return bool */ - protected function shouldStoreMessage(): bool + protected function shouldStoreMessage(Email $message): bool { - return property_exists($this->message, '_storageOptions') + return property_exists($message, '_storageOptions') && config('mail-export.enabled', false); } /** - * Actually stores the stringified version of the \Swift_Message including headers, - * recipients, subject and body onto the filesystem disk. + * Actually stores the stringified version of the \Symfony\Component\Mime\Email + * including headers, recipients, subject and body onto the filesystem disk. * * @return void */ - private function storeMessage() + private function storeMessage(Email $message) { /** @var StorageOptions $storageOptions */ - $storageOptions = $this->message->_storageOptions; + $storageOptions = $message->_storageOptions; $this->filesystem ->disk($storageOptions->disk) - ->put($storageOptions->fullpath(), $this->message->toString(), [ + ->put($storageOptions->fullpath(), $message->toString(), [ 'mimetype' => $storageOptions::MIME_TYPE, ]); - event(new MessageStored($this->message, $storageOptions)); + event(new MessageStored($message, $storageOptions)); } } diff --git a/src/StorageOptions.php b/src/StorageOptions.php index d50c0f3..2a697e1 100644 --- a/src/StorageOptions.php +++ b/src/StorageOptions.php @@ -5,11 +5,11 @@ use Carbon\Carbon; use Illuminate\Support\Arr; use Illuminate\Support\Str; -use Swift_Message; +use Symfony\Component\Mime\Email; /** * Data transfer object responsible for holding - * storage informations when exporting a mail. + * storage information when exporting a mail. */ class StorageOptions { @@ -32,18 +32,19 @@ class StorageOptions public $filename; /** - * @var \Swift_Message + * @var \Symfony\Component\Mime\Email */ public $message; /** - * Declares the storage options for a specific \Swift_Message. The only - * properties allowed are 'disk', 'path' and 'filename', all optional. + * Declares the storage options for a specific \Symfony\Component\Mime\Email. + * The only properties allowed are 'disk', 'path' and 'filename'. They all + * are obviously optional. * - * @param Swift_Message $message + * @param Email $message * @param array $properties */ - public function __construct(Swift_Message $message, array $properties = []) + public function __construct(Email $message, array $properties = []) { $this->message = $message; @@ -62,7 +63,7 @@ public function __construct(Swift_Message $message, array $properties = []) */ private function defaultDisk(): string { - return config('mail-export.disk') ?: config('filesystem.default'); + return config('mail-export.disk') ?: config('filesystems.default'); } /** @@ -83,10 +84,11 @@ private function defaultPath(): string */ private function defaultFilename(): string { - $recipients = array_keys($this->message->getTo()); + /** @var \Symfony\Component\Mime\Address[] */ + $recipients = $this->message->getTo(); $to = ! empty($recipients) - ? str_replace(['@', '.'], ['_at_', '_'], $recipients[0]).'_' + ? str_replace(['@', '.'], ['_at_', '_'], $recipients[0]->getAddress()).'_' : ''; $subject = $this->message->getSubject(); diff --git a/tests/Feature/MailExportTest.php b/tests/Feature/MailExportTest.php index 1097c3e..f1489c4 100644 --- a/tests/Feature/MailExportTest.php +++ b/tests/Feature/MailExportTest.php @@ -73,7 +73,7 @@ public function it_will_export_mails_as_eml_files() /** @test */ public function it_can_export_using_the_default_application_disk_if_none_is_given() { - config()->set('filesystem.default', 'default_disk'); + config()->set('filesystems.default', 'default_disk'); config()->set('mail-export.disk', null); Storage::fake('local');