black-lamp / yii2-email-templates
Module for adding templates for email letters across dashboard
Installs: 946
Dependents: 4
Suggesters: 0
Security: 0
Stars: 10
Watchers: 6
Forks: 6
Open Issues: 1
Type:yii2-extension
Requires
- php: >=5.6.0
- 2amigos/yii2-tinymce-widget: ~1.1
- yiisoft/yii2: ^2.0.4
- yiisoft/yii2-bootstrap: ^2.0.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-10-26 20:33:23 UTC
README
Module for adding templates for email letters across dashboard
Installation
Run command
composer require black-lamp/yii2-email-templates
or add
"black-lamp/yii2-email-templates": "^3.0.0"
to the require section of your composer.json.
Applying migrations
yii migrate --migrationPath=@vendor/black-lamp/yii2-email-templates/src/migrations
Add module to application config
Backend module for create, edit and delete email templates
'modules' => [ // ... 'email-templates' => [ 'class' => bl\emailTemplates\EmailTemplates::class, 'languageProvider' => [ 'class' => bl\emailTemplates\providers\DbLanguageProvider::class, 'tableName' => 'language', 'idField' => 'id', 'nameField' => 'name' ] ], ]
languageProvider
it's a class that implements LanguageProviderInterface.
You can use language providers from this extension or create yours.
This extension has two language providers.
Database language provider configuration properties
Config language provider configuration properties
Add component to application config
Component for getting the templates from database
'components' => [ // ... 'emailTemplates' => [ 'class' => bl\emailTemplates\components\TemplateManager::class ], ]
Using
- Create the template with markers across dashboard
Email subject
New message from {sitename}
Email body
Hello, {username}!
Text...
Go to the link - {link}
- Get template by key with component help
$template = Yii::$app->templateManager->getTemplate('test', 1);
This method return a Template object.
- You should to parse the markers in email subject and email body
$template->parseSubject([ '{sitename}' => $sitename ]); $template->parseBody([ '{username}' => Yii::$app->user->identity->firstname, '{link}' => Url::toRoute(['/confirm', 'token' => $token], true) ]);
- Now you can using this template
Yii::$app->mailer->compose() // ... ->setSubject($template->subject) ->setHtmlBody($template->body) // ...