|
20 | 20 | </a> |
21 | 21 | </p> |
22 | 22 |
|
23 | | -# Mail logger and viewer for Laravel |
| 23 | +# Laravel Mail Viewer |
| 24 | + |
| 25 | +Easily log, view, and search outgoing emails directly in your browser. |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +This package logs all outgoing emails to a database and provides a web interface to view them, formatted as they appear in modern email clients like Gmail. |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +<!-- TOC --> |
| 35 | +* [Laravel Mail Viewer](#laravel-mail-viewer) |
| 36 | + * [Features](#features) |
| 37 | + * [Installation](#installation) |
| 38 | + * [Step 1: Install via Composer](#step-1-install-via-composer) |
| 39 | + * [Step 2: Publish Assets & Configurations](#step-2-publish-assets--configurations) |
| 40 | + * [Step 3: Run Migrations](#step-3-run-migrations) |
| 41 | + * [Step 4: View Emails](#step-4-view-emails) |
| 42 | + * [Configuration](#configuration) |
| 43 | + * [Data Pruning](#data-pruning) |
| 44 | + * [Production Usage](#production-usage) |
| 45 | + * [Restrict Access with Middleware](#restrict-access-with-middleware) |
| 46 | + * [Disable package in production mode](#disable-package-in-production-mode) |
| 47 | + * [Disable auto-discovery:](#disable-auto-discovery) |
| 48 | + * [Register package for non-production environments](#register-package-for-non-production-environments) |
| 49 | + * [License](#license) |
| 50 | + * [Credits](#credits) |
| 51 | +<!-- TOC --> |
| 52 | + |
| 53 | +## Features |
| 54 | +- Logs all outgoing emails to the database |
| 55 | +- Modern in-browser email viewer |
| 56 | +- Searchable UI with auto-refreshing entries |
| 57 | +- Configurable route and access protection |
| 58 | +- Optional email pruning |
24 | 59 |
|
25 | | -### Easily log, view and search in browser all outgoing emails. |
| 60 | +## Installation |
| 61 | +### Step 1: Install via Composer |
| 62 | + |
| 63 | +Run the following command in your terminal: |
| 64 | + |
| 65 | +```sh |
| 66 | +composer require masterro/laravel-mail-viewer |
| 67 | +``` |
26 | 68 |
|
27 | | - |
| 69 | +### Step 2: Publish Assets & Configurations |
28 | 70 |
|
29 | | -This package gives an ability to log all outgoing emails to a database and view them all from a browser like they will |
30 | | -be shown in a modern mail clients (gmail, etc.). |
| 71 | +```sh |
| 72 | +php artisan mail-viewer:publish |
| 73 | +``` |
31 | 74 |
|
32 | | -## Version Compatibility |
| 75 | +### Step 3: Run Migrations |
33 | 76 |
|
34 | | -| Laravel | Mail Viewer | |
35 | | -|:------------|:------------| |
36 | | -| 5.5.x - 8.* | 1.3.x | |
37 | | -| 9.x - 10.x | 2.x.x | |
| 77 | +```sh |
| 78 | +php artisan migrate |
| 79 | +``` |
38 | 80 |
|
39 | | -## Upgrade from v1 to v2 |
| 81 | +### Step 4: View Emails |
40 | 82 |
|
41 | | -Version 2 has been almost totally rewritten and brings totally new fresh UI build with Vue.js 3 and TailwindCss 3. |
42 | | -It works **only with Laravel 9+** as of Symfony Mailer replacement for previously used Swift Mailer. |
| 83 | +Visit `/_mail-viewer` in your browser to access the email viewer. |
43 | 84 |
|
44 | | -### Upgrade Steps |
| 85 | +> **Note:** The route can be customized in the configuration file. |
45 | 86 |
|
46 | | -#### Composer Dependencies |
| 87 | +--- |
47 | 88 |
|
48 | | -You should update the dependency in your application's composer.json file: |
| 89 | +## Configuration |
49 | 90 |
|
50 | | -`masterro/laravel-mail-viewer` to ^2.0 |
| 91 | +You can adjust default settings in the `config/mail-viewer.php` file. |
51 | 92 |
|
52 | | -#### Database migrations |
| 93 | +### Data Pruning |
53 | 94 |
|
54 | | -Run package migrations (requires `doctrine/dbal` to be installed): |
| 95 | +The package supports Laravel's [Model Pruning](https://laravel.com/docs/eloquent#pruning-models). Define how many days emails should be retained in the configuration: |
55 | 96 |
|
56 | | -```shell |
57 | | -php artisan migrate |
| 97 | +```php |
| 98 | +'prune_older_than_days' => 365, |
58 | 99 | ``` |
59 | 100 |
|
60 | | -#### Publish assets |
| 101 | +--- |
61 | 102 |
|
62 | | -Run publish command: |
| 103 | +## Production Usage |
63 | 104 |
|
64 | | -```shell |
65 | | -php artisan mail-viewer:publish --views |
66 | | -``` |
| 105 | +By default, the email viewer is publicly accessible. |
| 106 | +In a production environment, it's highly recommended to restrict access |
| 107 | +using middleware or something like [Access Screen](https://github.com/MasterRO94/laravel-access-screen) package. |
| 108 | +Alternatively, you can disable the package in production environments. |
67 | 109 |
|
68 | | -#### Update configs |
| 110 | +### Restrict Access with Middleware |
69 | 111 |
|
70 | | -V2 uses separate date format for date and time, update these in your `config/mail-viewer.php` file |
| 112 | +Modify your `config/mail-viewer.php` to apply authorization: |
71 | 113 |
|
72 | 114 | ```php |
73 | | -'date_format' => 'd.m.Y', |
74 | | -'time_format' => 'H:i:s', |
| 115 | +'middleware' => ['web', 'can:viewMailLogs'], |
75 | 116 | ``` |
76 | 117 |
|
77 | | -#### Data pruning |
| 118 | +> **Note:** `viewMailLogs` is just an example ability you can register via Laravel’s [Authorization Gate](https://laravel.com/docs/authorization#writing-gates). |
| 119 | +> This ability is not included in the package. |
78 | 120 |
|
79 | | -V2 allows prune old records easily using `mail-viewer:prune` command. You can add it to your Scheduler. |
| 121 | +You can also limit access by IP address in `App\Http\Middleware\RestrictMailViewerAccess.php`: |
80 | 122 |
|
81 | 123 | ```php |
82 | | -// Console/Kernel.php |
83 | | -$schedule->command('mail-viewer:prune')->daily(); |
| 124 | +namespace App\Http\Middleware; |
| 125 | + |
| 126 | +use Closure; |
| 127 | +use Illuminate\Http\Request; |
| 128 | + |
| 129 | +class RestrictMailViewerAccess |
| 130 | +{ |
| 131 | + public function handle(Request $request, Closure $next) |
| 132 | + { |
| 133 | + if (!in_array($request->ip(), ['127.0.0.1', '::1', 'YOUR_ALLOWED_IP'])) { |
| 134 | + abort(403); |
| 135 | + } |
| 136 | + |
| 137 | + return $next($request); |
| 138 | + } |
| 139 | +} |
84 | 140 | ``` |
85 | 141 |
|
86 | | -You can specify how many days data will be stored before pruning using config. Default value is 31 days. |
| 142 | +Apply it in config: |
87 | 143 |
|
88 | 144 | ```php |
89 | | -'prune_older_than_days' => 31, |
| 145 | +'middleware' => ['web', RestrictMailViewerAccess::class], |
90 | 146 | ``` |
91 | 147 |
|
92 | | -## Installation |
93 | | - |
94 | | -### Step 1: Composer |
| 148 | +Now, only authorized users or allowed IPs can access the mail viewer. |
95 | 149 |
|
96 | | -From the command line, run: |
| 150 | +--- |
97 | 151 |
|
| 152 | +### Disable package in production mode |
| 153 | +#### Disable auto-discovery: |
| 154 | +```json |
| 155 | +"extra": { |
| 156 | + "laravel": { |
| 157 | + "dont-discover": [ |
| 158 | + "masterro/laravel-mail-viewer" |
| 159 | + ] |
| 160 | + } |
| 161 | +}, |
98 | 162 | ``` |
99 | | -composer require masterro/laravel-mail-viewer |
100 | | -``` |
101 | | - |
102 | | -### Step 2: Publish assets and configs |
103 | | - |
104 | | -``` |
105 | | -php artisan mail-viewer:publish |
106 | | -``` |
107 | | - |
108 | | -You have to publish _**assets,**_ and _**views,**_ _configs_ are optional. |
109 | | - |
110 | | -### Step 3: Run migrations |
111 | 163 |
|
112 | | -``` |
113 | | -php artisan migrate |
| 164 | +#### Register package for non-production environments |
| 165 | +In your application's `ServiceProvider` |
| 166 | +```php |
| 167 | +use MasterRO\MailViewer\Providers\MailViewerServiceProvider; |
| 168 | + |
| 169 | +public function register(): void |
| 170 | +{ |
| 171 | + if (!$this->app->environment('production')) { |
| 172 | + $this->app->register(MailViewerServiceProvider::class); |
| 173 | + } |
| 174 | +} |
114 | 175 | ``` |
115 | 176 |
|
116 | | -### Step 4: View emails |
| 177 | +## License |
117 | 178 |
|
118 | | -All ongoing emails you can find on `/_mail-viewer` page. |
| 179 | +This package is open-source software licensed under the [MIT license](LICENSE). |
119 | 180 |
|
120 | | -## Configuration |
| 181 | +--- |
121 | 182 |
|
122 | | -You can review and change all the default configuration values in published `config/mail-viewer.php` file. |
| 183 | +## Credits |
123 | 184 |
|
124 | | -#### Data pruning (v2+) |
| 185 | +Developed by [MasterRO](https://github.com/MasterRO94). |
125 | 186 |
|
126 | | -The package allows you to prune old records easily using `mail-viewer:prune` command. You can add it to your Scheduler. |
127 | | - |
128 | | -```php |
129 | | -// Console/Kernel.php |
130 | | -$schedule->command('mail-viewer:prune')->daily(); |
131 | | -``` |
|
0 commit comments