Skip to content

Extension for creating email templates and manage using your site dashboard

License

Notifications You must be signed in to change notification settings

kharalampidi/yii2-email-templates

 
 

Repository files navigation

Email templates module (fork)


Extension for creating email templates and managing by using your site dashboard. You can create email templates with CRUD module in your backend or Gii generator.

Documentation is at docs/guide/README.md.

SymfonyInsight

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version

Installation

The preferred way to install this extension is through composer.

Either run

$ composer require yiimaker/yii2-email-templates

or add

"yiimaker/yii2-email-templates": "~4.1"

to the require section of your composer.json.

Usage

  1. Create template with placeholders using your site dashboard or Gii generator

    Key

    register-notification - this is unique key of this template for using in your code

    Subject

    Notification from {siteName}

    In this example email subject has one placeholder {siteName}

    Body

    Hello, {username}! Welcome to {siteName} :)

    Email body has two placeholders: {username} and {siteName}.

    All keys should be wrapped by {}.

  2. Now you can get this template in your code

    $template = Yii::$app->get('templateManager')->getTemplate('register-notification');

    This method returns a template model object.

  3. Create a class that implements the ymaker\email\templates\templates interface.

    #####Property names are equal to placeholders.

     use ymaker\email\templates\templates\TemplateInterface;
     
     class NotificationTemplate implements TemplateInterface
     {
         /**
          * @var string
          */
         private $username;
         /**
          * @var string
          */
         private $siteName;
     
         public function __construct(string $username, string $siteName)
         {
     
             $this->username = $username;
             $this->siteName = $siteName;
         }
     }
  4. Then you should parse this template

    $template->parse(new NotificationTemplate(
        Yii::$app->getIdentity()->username,
        Yii::$app->name
    ));

    this methods replace placeholders in template with real data.

  5. Now you can use data of this template in your logic

    Yii::$app->get('mailer')->compose()
        ->setSubject($template->subject())
        ->setHtmlBody($template->body())
        // ...

Tests

You can run tests with composer command

$ composer test

or using following command

$ codecept build && codecept run

Contributing

For information about contributing please read CONTRIBUTING.md.

License

License

This project is released under the terms of the BSD-3-Clause license.

Copyright (c) 2017-2018, Yii Maker

About

Extension for creating email templates and manage using your site dashboard

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.0%
  • TSQL 1.0%