Download the PHP package statikbe/laravel-mail-template-engine without Composer
On this page you can find all versions of the php package statikbe/laravel-mail-template-engine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-mail-template-engine
Laravel Mail Template Engine
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:
-
Publish the mail template migration and config
- Run the mail template migration
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
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
.
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:
Name | Type | Description | Example |
---|---|---|---|
mail_class | string | A mail class from config mail-template-engine.mails |
verify-email |
name | string | Just a descriptive naming of the mail for an index page | Email verification mail |
subject | json (translatable) | The subject, fillable with ContentVariables from the corresponding Mail Class |
{"en": "Welcome [[name]], please verify your email"} |
body | json (translatable) | The body, fillable with ContentVariables from the corresponding Mail Class |
{"en": "Welcome [[name]], please verify your email using the following url [[url]]."} |
sender_name | json (translatable) | The sender by name | {"en": "Statik"} |
sender_email | json (translatable) | The sender by email | {"en": "[email protected]"} |
recipients | json | The recipients of the mail, an array filled with both RecipientVariables from the Mail Class and/or plain emails |
["user","[email protected]"] |
cc | json | The cc of the mail, an array filled with both RecipientVariables from the Mail Class and/or plain emails |
["user","[email protected]"] |
bcc | json | The bcc of the mail, an array filled with both RecipientVariables from the Mail Class and/or plain emails |
["user","[email protected]"] |
attachments | json (translatable) | work in progress | |
design | string | A design from config mail-template-engine.designs |
statikbe::mail.designs.default |
render_engine | string | A render engine from config mail-template-engine.render_engines |
html |
created_at | timestamp | ||
updated_at | timestamp | ||
deleted_at | timestamp |
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:
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!
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:
The following configuration fields are available:
TODO document configuration
License
The MIT License (MIT). Please see license file for more information.
All versions of laravel-mail-template-engine with dependencies
illuminate/mail Version ^10.0
illuminate/console Version ^10.0
illuminate/bus Version ^10.0
spatie/laravel-translatable Version ^6.0