statikbe / laravel-mail-template-engine
Customizable mail templates for Laravel
Installs: 1 075
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- php: ^8.0|^8.1
- illuminate/bus: ^10.0
- illuminate/console: ^10.0
- illuminate/mail: ^10.0
- spatie/laravel-translatable: ^6.0
This package is auto-updated.
Last update: 2024-12-15 10:38:54 UTC
README
The Laravel Mail Template Engine makes it possible to create mail templates for your transactional mails. These mail templates can hold content and recipient variables which can be filled when triggering the mail.
This package works together with our Laravel Nova Mail Editor that provides a UI to let content managers edit translations.
How it works
The packages combines 3 different sets of data to send a transactional mail.
- Firstly there is the Mail class: here you define a name, content variables and recipient variables.
- Next up you store your mail templates in your database, you can do this in Nova using Laravel Nova Mail Editor. Or you write your own interface to save the templates.
- Finally, you call the mail class and fill in its variables (content and recipient). At this moment the engine will look for all templates using this class, starts filling them with the provided variables and send them to the selected recipients.
Installation
- Install using composer:
composer require statikbe/laravel-mail-template-engine
- Publish the mail template migration and config
php artisan vendor:publish --tag=mail-template-engine
- Run the mail template migration
php artisan migrate
How to use
The mail class
This class will decide what content variables are available in your mail template and will be used to decide when a mail is send.
You can generate a mail with the following command
php artisan make:mail-class SomeEventMail
After creating a Mail class, add them to the config mail-template-engine.php
mails.
An example of a Mail class can be found at src/Mails/ResetPassword.php
.
<?php namespace Statikbe\LaravelMailEditor\Mails; use Statikbe\LaravelMailEditor\AbstractMail; class ResetPasswordMail extends AbstractMail { public static function name(){ return __('ResetPasswordTemplate'); } public static function getContentVariables(){ return [ 'url' => __('Reset password URL'), ]; } public static function getRecipientVariables(){ return [ 'user' => __('User'), ]; } }
The Mail Templates
Mail templates are stored in the database. This uses the model Statikbe\LaravelMailEditor\MailTemplate
and can be created like any other model.
A mail template can be localized, we use Spatie's translatable package for this to work. More information can be found on their page: Laravel Translatable.
You can use the keys from your corresponding mail_class to place variable data inside the mail template.
The MailTemplate
model consists off:
If your application is using Nova you can use Nova Mail Editor: a tool for editing and creating mail templates.
Calling the mail class
In order to send mails you provide the content and recipient variables, and then send Mail object.
The mail object will now look for any mail template in the database using this Mail class, build it and send the mail.
An example:
use Statikbe\LaravelMailEditor\Mails\ResetPasswordMail; $contentVars = [ 'nl' => [ //Optional wrap with locale 'url' => $verificationUrl, ], ]; $recipientVars = [ 'user' => [ //Use an array with mail and optional locale [ 'mail' => $user->email, 'locale' => 'en', ], //Or use an object $user ] ]; $attachments = [ 'nl' => [ //Optional wrap with locale 'user' => [ //Using laravel mail attach functionality [ 'path' => $path, 'options' => [ 'as' => 'factuur.pdf', 'mime' => 'application/pdf', ] ], //Or add an entire file $file ], ] ]; $mail = new ResetPasswordMail(); $mail->sendMail($contentVars, $recipientVars);
Content variables
In this array you assign the values to each key used in the mail template(s). You define the recipients in getContentVariables
in your mail class. Wrapping the content variables with a locale as key is optional.
Recipient variables
In this array you assign the mail and locale for each recipient in the mail template(s). You define the recipients in getRecipientVariables
in your mail class. For more options on how to add the recipient variables look at \Statikbe\LaravelMailEditor\AbstractMail::formatRecipientArray
.
Attachment variables
In this array you assign the attachment for each recipient in the mail template(s). You define the recipients in getRecipientVariables
in your mail class. For more options on how to add the recipient variables look at \Statikbe\LaravelMailEditor\AbsractMail::formatRecipientArray
. Wrapping the attachments with a locale as key is optional.
Styling
The default design provided by the package comes from here. You can publish the views and customise it all you want!
php artisan vendor:publish --provider="Statikbe\LaravelMailEditor\MailEditorServiceProvider" --tag=views
You can provide your own designs by adding them to the designs
array in the config. Designs are view directories where you store the views for your render engines, F.E. the default HTML engine expects a view called html.blade.php
.
Configuration
You can publish the configuration by running this command:
php artisan vendor:publish --tag=mail-template-engine
The following configuration fields are available:
TODO document configuration
License
The MIT License (MIT). Please see license file for more information.