charlielangridge/laravel-mail-previewer
Composer 安装命令:
composer require charlielangridge/laravel-mail-previewer
包简介
Laravel Mail Previewer - allows you to preview mailables and notifications with data
README 文档
README
Discover mailables and notifications in a Laravel app, inspect required inputs, and render preview HTML safely.
This package is designed for internal preview tooling and admin UIs.
Features
- Discover all app mailables and notifications.
- Return structured metadata:
- short
name - full
class subject(including dynamic placeholder parsing)input_requirements
- short
- Resolve required constructor input for a selected class.
- For model-typed inputs, return up to 100 selectable DB options.
- Render HTML previews for mailables and notifications.
- Never sends email when rendering previews.
Requirements
- PHP
^8.4 - PHP
^8.3 - Laravel
^11.0 || ^12.0
Installation
composer require charlielangridge/laravel-mail-previewer
The package auto-registers via Laravel package discovery.
Quick Start
use Charlielangridge\LaravelMailPreviewer\Facades\LaravelMailPreviewer; $list = LaravelMailPreviewer::discover(); $requirements = LaravelMailPreviewer::inputRequirements( \App\Mail\FormCompletion::class ); $issues = LaravelMailPreviewer::inputTypeHintingIssues(); $html = LaravelMailPreviewer::renderHtml( \App\Mail\FormCompletion::class, ['form' => 12] );
API
LaravelMailPreviewer::discover(): array
Returns both mailables and notifications:
[
'mailables' => [
[
'name' => 'FormCompletion',
'class' => 'App\\Mail\\FormCompletion',
'subject' => 'Hello **user->name**',
'input_requirements' => [
['name' => 'form', 'type' => 'App\\Models\\Forms\\Form'],
],
],
],
'notifications' => [
// same shape
],
]
Subject extraction rules:
- Uses class-level default
subjectwhen available. - Parses common subject definitions from class source:
Envelope(subject: ...)->subject(...)
- Resolves local variables where possible:
$subject = 'Test'; ->subject($subject)becomesTest
- External/runtime references are converted to placeholders:
'Hello '.$this->user->namebecomesHello **user->name**
LaravelMailPreviewer::inputRequirements(string $className): array
Returns required constructor parameters for a mailable/notification.
If parameter type is an Eloquent model, includes options from DB (limit 100):
[
[
'name' => 'form',
'type' => 'App\\Models\\Forms\\Form',
'options' => [
['id' => 12, 'label' => 'Access Training Feedback'],
// ...
],
],
[
'name' => 'token',
'type' => 'string',
],
]
Notes:
- Non-model parameters do not include
options. - Unsupported classes return an empty array.
LaravelMailPreviewer::inputTypeHintingIssues(): array
Returns mailables/notifications where required constructor inputs are not properly type-hinted
(missing type hint or resolved as mixed), so you can work through and fix them.
[
'mailables' => [
[
'kind' => 'mailable',
'name' => 'FormCompletion',
'class' => 'App\\Mail\\FormCompletion',
'untyped_input_requirements' => [
[
'name' => 'form',
'current_type' => 'mixed',
'has_type_hint' => false,
'suggested_type' => 'App\\Models\\Forms\\Form',
'suggestion_reason' => 'parameter name matches an app model class name',
],
],
],
],
'notifications' => [
// same shape
],
]
Suggestion strategy:
- Uses default parameter value type when present.
- Attempts to map parameter names to app model classes.
- Applies name heuristics (
...Id=>int,is...=>bool,token/email/name/...=>string). - Falls back to
string.
LaravelMailPreviewer::renderHtml(string $className, array $parameters = [], mixed $notifiable = null): ?string
Renders HTML preview for a mailable or notification using Laravel's rendering pipeline.
$html = LaravelMailPreviewer::renderHtml( \App\Notifications\FormCompleted::class, ['form' => 12] );
For model-typed constructor inputs, scalar values are treated as primary keys and resolved via findOrFail.
$notifiable is optional for notifications. If omitted, an internal anonymous notifiable is used.
Important:
- This method does not call send/notify pathways.
- It is for preview rendering only.
Tinker Examples
use Charlielangridge\LaravelMailPreviewer\Facades\LaravelMailPreviewer; // 1) list discoverable items LaravelMailPreviewer::discover(); // 2) get inputs for chosen class LaravelMailPreviewer::inputRequirements(\App\Mail\FormCompletion::class); // 3) audit classes with weak constructor type hints LaravelMailPreviewer::inputTypeHintingIssues(); // 4) render html preview with selected values $html = LaravelMailPreviewer::renderHtml( \App\Mail\FormCompletion::class, ['form' => 12] ); $html;
Testing
composer test
Changelog
Please see CHANGELOG for details.
License
MIT. See LICENSE.md.
统计信息
- 总下载量: 553
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-25