|
20 | 20 | </a> |
21 | 21 | </p> |
22 | 22 |
|
23 | | -# Mail logger and viewer for Laravel |
| 23 | +# Laravel Mail Logger & Viewer |
24 | 24 |
|
25 | | -### Easily log, view and search in browser all outgoing emails. |
| 25 | +Easily log, view, and search outgoing emails directly in your browser. |
26 | 26 |
|
27 | | - |
| 27 | + |
| 28 | + |
28 | 29 |
|
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.). |
| 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 | 31 |
|
32 | | -## Version Compatibility |
| 32 | +--- |
33 | 33 |
|
34 | | -| Laravel | Mail Viewer | |
35 | | -|:------------|:------------| |
36 | | -| 5.5.x - 8.* | 1.3.x | |
37 | | -| 9.x - 12.x | 2.x.x | |
38 | | -| 12.x | 3.x.x | |
39 | | - |
40 | | -## Upgrade from v1 to v2 |
41 | | - |
42 | | -Version 2 has been almost totally rewritten and brings totally new fresh UI build with Vue.js 3 and TailwindCss 3. |
43 | | -It works **only with Laravel 9+** as of Symfony Mailer replacement for previously used Swift Mailer. |
| 34 | +## Installation |
44 | 35 |
|
45 | | -### Upgrade Steps |
| 36 | +### Step 1: Install via Composer |
46 | 37 |
|
47 | | -#### Composer Dependencies |
| 38 | +Run the following command in your terminal: |
48 | 39 |
|
49 | | -You should update the dependency in your application's composer.json file: |
| 40 | +```sh |
| 41 | +composer require masterro/laravel-mail-viewer |
| 42 | +``` |
50 | 43 |
|
51 | | -`masterro/laravel-mail-viewer` to ^2.0 |
| 44 | +### Step 2: Publish Assets & Configurations |
52 | 45 |
|
53 | | -#### Database migrations |
| 46 | +```sh |
| 47 | +php artisan mail-viewer:publish |
| 48 | +``` |
54 | 49 |
|
55 | | -Run package migrations (requires `doctrine/dbal` to be installed): |
| 50 | +### Step 3: Run Migrations |
56 | 51 |
|
57 | | -```shell |
| 52 | +```sh |
58 | 53 | php artisan migrate |
59 | 54 | ``` |
60 | 55 |
|
61 | | -#### Publish assets |
| 56 | +### Step 4: View Emails |
62 | 57 |
|
63 | | -Run publish command: |
| 58 | +Visit `/_mail-viewer` in your browser to access the email viewer. |
64 | 59 |
|
65 | | -```shell |
66 | | -php artisan mail-viewer:publish --views |
67 | | -``` |
| 60 | +> **Note:** The route can be customized in the configuration file. |
68 | 61 |
|
69 | | -#### Update configs |
| 62 | +--- |
70 | 63 |
|
71 | | -V2 uses separate date format for date and time, update these in your `config/mail-viewer.php` file |
| 64 | +## Configuration |
72 | 65 |
|
73 | | -```php |
74 | | -'date_format' => 'd.m.Y', |
75 | | -'time_format' => 'H:i:s', |
76 | | -``` |
| 66 | +You can adjust default settings in the `config/mail-viewer.php` file. |
77 | 67 |
|
78 | | -#### Data pruning |
| 68 | +### Data Pruning |
79 | 69 |
|
80 | | -V2 allows prune old records easily using `mail-viewer:prune` command. You can add it to your Scheduler. |
| 70 | +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: |
81 | 71 |
|
82 | 72 | ```php |
83 | | -// Console/Kernel.php |
84 | | -$schedule->command('mail-viewer:prune')->daily(); |
| 73 | +'prune_older_than_days' => 365, |
85 | 74 | ``` |
86 | 75 |
|
87 | | -You can specify how many days data will be stored before pruning using config. Default value is 31 days. |
| 76 | +--- |
88 | 77 |
|
89 | | -```php |
90 | | -'prune_older_than_days' => 31, |
91 | | -``` |
| 78 | +## Production Usage |
92 | 79 |
|
93 | | -## Installation |
| 80 | +By default, the email viewer is publicly accessible. |
| 81 | +In a production environment, it's highly recommended to restrict access |
| 82 | +using middleware or something like [Access Screen](https://github.com/MasterRO94/laravel-access-screen) package. |
94 | 83 |
|
95 | | -### Step 1: Composer |
| 84 | +### Restrict Access with Middleware |
96 | 85 |
|
97 | | -From the command line, run: |
| 86 | +Modify your `routes/web.php` to apply authentication: |
98 | 87 |
|
99 | | -``` |
100 | | -composer require masterro/laravel-mail-viewer |
| 88 | +```php |
| 89 | +'middleware' => ['web', 'can:viewMailLogs'], |
101 | 90 | ``` |
102 | 91 |
|
103 | | -### Step 2: Publish assets and configs |
| 92 | +You can also limit access by IP address in `App\Http\Middleware\RestrictMailViewerAccess.php`: |
104 | 93 |
|
105 | | -``` |
106 | | -php artisan mail-viewer:publish |
| 94 | +```php |
| 95 | +namespace App\Http\Middleware; |
| 96 | + |
| 97 | +use Closure; |
| 98 | +use Illuminate\Http\Request; |
| 99 | + |
| 100 | +class RestrictMailViewerAccess |
| 101 | +{ |
| 102 | + public function handle(Request $request, Closure $next) |
| 103 | + { |
| 104 | + if (!in_array($request->ip(), ['127.0.0.1', '::1', 'YOUR_ALLOWED_IP'])) { |
| 105 | + abort(403); |
| 106 | + } |
| 107 | + |
| 108 | + return $next($request); |
| 109 | + } |
| 110 | +} |
107 | 111 | ``` |
108 | 112 |
|
109 | | -You have to publish _**assets,**_ and _**views,**_ _configs_ are optional. |
| 113 | +Apply it in config: |
110 | 114 |
|
111 | | -### Step 3: Run migrations |
| 115 | +```php |
112 | 116 |
|
113 | | -``` |
114 | | -php artisan migrate |
| 117 | +'middleware' => ['web', RestrictMailViewerAccess::class], |
115 | 118 | ``` |
116 | 119 |
|
117 | | -### Step 4: View emails |
| 120 | +Now, only authorized users or allowed IPs can access the mail viewer. |
118 | 121 |
|
119 | | -All ongoing emails you can find on `/_mail-viewer` page. |
| 122 | +--- |
120 | 123 |
|
121 | | -## Configuration |
| 124 | +## License |
122 | 125 |
|
123 | | -You can review and change all the default configuration values in published `config/mail-viewer.php` file. |
| 126 | +This package is open-source software licensed under the [MIT license](LICENSE). |
124 | 127 |
|
125 | | -#### Data pruning (v2+) |
| 128 | +--- |
126 | 129 |
|
127 | | -The package allows you to prune old records easily using `mail-viewer:prune` command. You can add it to your Scheduler. |
| 130 | +## Credits |
| 131 | + |
| 132 | +Developed by [MasterRO](https://github.com/MasterRO94). |
128 | 133 |
|
129 | | -```php |
130 | | -// Console/Kernel.php |
131 | | -$schedule->command('mail-viewer:prune')->daily(); |
132 | | -``` |
|
0 commit comments