-
Create template with placeholders using your site dashboard or Gii generator
register-notification
- this is unique key of this template for using in your codeNotification from {site-name}
In this example email subject has one placeholder
{site-name}
Hello, {username}! Welcome to {site-name} :)
Email body has two placeholders:
{username}
and{site-name}
.All keys should be wrapped by
{}
. -
Now you can get this template in your code
$template = Yii::$app->get('templateManager')->getTemplate('register-notification');
This method returns a template model object.
-
Then you should parse this template
$template->parseSubject([ 'site-name' => Yii::$app->name, ]); $template->parseBody([ 'username' => Yii::$app->getIdentity()->username, 'site-name' => Yii::$app->name, ]);
or use another method
$template->parse([ 'subject' => [ 'site-name' => Yii::$app->name, ], 'body' => [ 'username' => Yii::$app->getIdentity()->username, 'site-name' => Yii::$app->name, ], ]);
this methods replace placeholders in template with real data.
-
Now you can use data of this template in your logic
Yii::$app->get('mailer')->compose() ->setSubject($template->subject) ->setHtmlBody($template->body) // ...