-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc1c855
commit 06813aa
Showing
9 changed files
with
164 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
# A package to create customizable mail templates in Filament | ||
# Mail Templates for Filament | ||
|
||
## | ||
|
||
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. | ||
A package to quickly create customizable mail templates in Filament | ||
|
||
## Installation | ||
|
||
|
@@ -12,65 +10,12 @@ You can install the package via composer: | |
composer require codedor/filament-mail-templates | ||
``` | ||
|
||
You can publish and run the migrations with: | ||
|
||
```bash | ||
php artisan vendor:publish --tag="filament-mail-templates-migrations" | ||
php artisan migrate | ||
``` | ||
|
||
You can publish the config file with: | ||
|
||
```bash | ||
php artisan vendor:publish --tag="filament-mail-templates-config" | ||
``` | ||
|
||
This is the contents of the published config file: | ||
|
||
```php | ||
return [ | ||
]; | ||
``` | ||
|
||
Optionally, you can publish the views using | ||
|
||
```bash | ||
php artisan vendor:publish --tag="filament-mail-templates-views" | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
$filamentMailTemplates = new Codedor\FilamentMailTemplates(); | ||
echo $filamentMailTemplates->echoPhrase('Hello, Codedor!'); | ||
``` | ||
|
||
## Documentation | ||
|
||
For the full documentation, check [here](./docs/index.md). | ||
|
||
## Testing | ||
|
||
```bash | ||
vendor/bin/pest | ||
``` | ||
|
||
## Changelog | ||
|
||
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. | ||
|
||
## Upgrading | ||
|
||
Please see [UPGRADING](UPGRADING.md) for more information on how to upgrade to a new version. | ||
|
||
## Contributing | ||
|
||
Please see [CONTRIBUTING](CONTRIBUTING.md) for details. | ||
|
||
## Security Vulnerabilities | ||
|
||
If you discover any security-related issues, please email [email protected] instead of using the issue tracker. | ||
|
||
## License | ||
|
||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,144 @@ | ||
# A package to create customizable mail templates in Filament | ||
# Mail Templates for Filament | ||
|
||
## Introduction | ||
A package to quickly create customizable mail templates in Filament | ||
|
||
## Installation | ||
|
||
You can install the package via composer: | ||
|
||
```bash | ||
composer require codedor/filament-mail-templates | ||
``` | ||
|
||
## Defining Mail Templates | ||
|
||
When adding mail templates there are a few things to do, first you'll need to add the `Codedor\FilamentMailTemplates\Models\Traits\HasMails` trait models that you want to have mail templates. | ||
|
||
```php | ||
namespace App\Models; | ||
|
||
use Codedor\FilamentMailTemplates\Models\Traits\HasMails; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Inquiry extends Model | ||
{ | ||
use HasMails; | ||
} | ||
``` | ||
|
||
After doing this, you'll also need to define what fields you want to be able to use in the mail template. To do this you'll need to define the fields in the `getMailVariables` method. | ||
|
||
The keys in this array will be the variables that show up in the mail template as labels, and the values will be the values that will be used in the mail template, when sent. | ||
|
||
```php | ||
namespace App\Models; | ||
|
||
use Codedor\FilamentMailTemplates\Models\Traits\HasMails; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Inquiry extends Model | ||
{ | ||
use HasMails; | ||
|
||
public function getMailVariables(): array | ||
{ | ||
return [ | ||
'first name' => $this->first_name, | ||
'last name' => $this->last_name, | ||
'e-mail' => $this->email, | ||
'message body' => $this->message, | ||
'inquiry type' => $this->inquiryType?->working_title, | ||
]; | ||
} | ||
|
||
public function inquiryType() | ||
{ | ||
return $this->belongsTo(InquiryType::class); | ||
} | ||
} | ||
``` | ||
|
||
Lastly, you need to define the actual mail template. To do this you'll need to add the following to any Provider in your project: | ||
|
||
```php | ||
use App\Models; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class AppServiceProvider extends ServiceProvider | ||
{ | ||
public function boot(): void | ||
{ | ||
Models\Inquiry::registerMail('inquiry-admin') | ||
->description('Send an email to the admin when a new inquiry is made'); | ||
|
||
Models\Inquiry::registerMail('inquiry-user') | ||
->view('mail.inquiry-new') | ||
->description('Send an email to the user when a new inquiry is made'); | ||
} | ||
} | ||
``` | ||
|
||
The `registerMail` method takes a string as the first argument, this is the name/identifier of the mail template and should be kebabcase. A description can be given as well, this is optional but will provide the admin additional information about the mail template in the Filament resource. | ||
|
||
## Saving the Mail Templates | ||
|
||
When you are done defining your templates, run the following command: | ||
|
||
```bash | ||
php artisan filament-mail-templates:create | ||
``` | ||
|
||
This will loop over all your defined mail templates and create them in the database. It is recommended that you add this command in your deployment process, so that the mail templates are always up to date. | ||
|
||
## Sending Mail Templates from the front-end | ||
|
||
After filling in all data in the CMS, you can get the mail object in the front-end by using the `mail` method of the model. For example, this contact form: | ||
|
||
```php | ||
class ContactForm extends Component | ||
{ | ||
public array $fields = []; | ||
|
||
public function submit() | ||
{ | ||
$this->validate(); | ||
|
||
/** @var Inquiry $inquiry */ | ||
$inquiry = Inquiry::create($this->fields); | ||
|
||
Mail::send($inquiry->mail('inquiry-admin')->to($inquiry->inquiryType?->to_email)); | ||
Mail::send($inquiry->mail('inquiry-user')->to($inquiry->email)); | ||
} | ||
} | ||
``` | ||
|
||
This example will send a mail to the admin and the user. | ||
|
||
The `mail` method returns a Laravel Mailable object, so you can use all the methods that are available on that object. | ||
|
||
## Customizing Mail Templates | ||
|
||
The config file comes with the following: | ||
|
||
```php | ||
return [ | ||
'default' => [ | ||
'view' => 'filament-mail-templates::mail.template', | ||
'to_email' => null, | ||
], | ||
'navigation' => [ | ||
'templates' => [ | ||
'icon' => 'heroicon-o-inbox', | ||
'group' => 'Mails', | ||
'shown' => true, | ||
], | ||
'history' => [ | ||
'icon' => 'heroicon-o-mail', | ||
'group' => 'Mails', | ||
'shown' => true, | ||
], | ||
], | ||
]; | ||
``` | ||
|
||
Here you can define some default values and customize the navigation icon/groups of the resources. The `shown` key can be used to hide the resource from the navigation completely. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters