Commit a2a60282 by Qiang Xue

Fixes #3992

Fixes #4147
parent bced4697
<?php <?php
use yii\helpers\Html; use yii\helpers\Html;
/* @var $this \yii\web\View */ /* @var $this \yii\web\View view component instance */
/* @var $content string */ /* @var $message \yii\mail\MessageInterface the message bing composed */
/* @var $content string main view render result */
?> ?>
<?php $this->beginPage() ?> <?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
......
<?php <?php
use yii\helpers\Html; use yii\helpers\Html;
/* @var $this \yii\web\View */ /* @var $this \yii\web\View view component instance */
/* @var $content string */ /* @var $message \yii\mail\MessageInterface the message bing composed */
/* @var $content string main view render result */
?> ?>
<?php $this->beginPage() ?> <?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
......
...@@ -141,6 +141,7 @@ Layout can be used to setup mail CSS styles or other shared content: ...@@ -141,6 +141,7 @@ Layout can be used to setup mail CSS styles or other shared content:
use yii\helpers\Html; use yii\helpers\Html;
/* @var $this \yii\web\View view component instance */ /* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message bing composed */
/* @var $content string main view render result */ /* @var $content string main view render result */
?> ?>
<?php $this->beginPage() ?> <?php $this->beginPage() ?>
...@@ -227,4 +228,4 @@ another one for the 'Message'. ...@@ -227,4 +228,4 @@ another one for the 'Message'.
You can use `yii\mail\BaseMailer` and `yii\mail\BaseMessage` as a base classes for your solution. These classes You can use `yii\mail\BaseMailer` and `yii\mail\BaseMessage` as a base classes for your solution. These classes
already contains basic logic, which is described in this guide. However, their usage is not mandatory, it is enough already contains basic logic, which is described in this guide. However, their usage is not mandatory, it is enough
to implement `yii\mail\MailerInterface` and `yii\mail\MessageInterface` interfaces. to implement `yii\mail\MailerInterface` and `yii\mail\MessageInterface` interfaces.
Then you need to implement all abstract methods to build you solution. Then you need to implement all abstract methods to build you solution.
\ No newline at end of file
...@@ -121,6 +121,7 @@ Yii Framework 2 Change Log ...@@ -121,6 +121,7 @@ Yii Framework 2 Change Log
- Removed character maps for non-latin languages. - Removed character maps for non-latin languages.
- Improved overall slug results. - Improved overall slug results.
- Added note about the fact that intl is required for non-latin languages to requirements checker. - Added note about the fact that intl is required for non-latin languages to requirements checker.
- Enh #3992: In mail layouts you can now access the message object via `$message` variable (qiangxue)
- Enh #4028: Added ability to `yii\widgets\Menu` to encode each item's label separately (creocoder, umneeq) - Enh #4028: Added ability to `yii\widgets\Menu` to encode each item's label separately (creocoder, umneeq)
- Enh #4072: `\yii\rbac\PhpManager` adjustments (samdark) - Enh #4072: `\yii\rbac\PhpManager` adjustments (samdark)
- Data is now stored in three separate files for items, assignments and rules. File format is simpler. - Data is now stored in three separate files for items, assignments and rules. File format is simpler.
...@@ -163,6 +164,7 @@ Yii Framework 2 Change Log ...@@ -163,6 +164,7 @@ Yii Framework 2 Change Log
- Chg #3956: Flash messages set via `Yii::$app->session->setFlash()` will be removed only if they are accessed (qiangxue) - Chg #3956: Flash messages set via `Yii::$app->session->setFlash()` will be removed only if they are accessed (qiangxue)
- Chg #3989: The default value for `yii\log\FileTarget::$rotateByCopy` now defaults to true to work on windows by default (cebe) - Chg #3989: The default value for `yii\log\FileTarget::$rotateByCopy` now defaults to true to work on windows by default (cebe)
- Chg #4071: `mail` component renamed to `mailer`, `yii\log\EmailTarget::$mail` renamed to `yii\log\EmailTarget::$mailer` (samdark) - Chg #4071: `mail` component renamed to `mailer`, `yii\log\EmailTarget::$mail` renamed to `yii\log\EmailTarget::$mailer` (samdark)
- Chg #4147: `BaseMailer::compose()` will not overwrite the `message` parameter if it is explicitly provided (qiangxue)
- Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue) - Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
- Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue) - Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue)
- Chg: `yii\grid\DataColumn::getDataCellValue()` visibility is now `public` to allow accessing the value from a GridView directly (cebe) - Chg: `yii\grid\DataColumn::getDataCellValue()` visibility is now `public` to allow accessing the value from a GridView directly (cebe)
......
...@@ -144,6 +144,8 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -144,6 +144,8 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
return Yii::createObject($config); return Yii::createObject($config);
} }
private $_message;
/** /**
* Creates a new message instance and optionally composes its body content via view rendering. * Creates a new message instance and optionally composes its body content via view rendering.
* *
...@@ -167,30 +169,41 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -167,30 +169,41 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
public function compose($view = null, array $params = []) public function compose($view = null, array $params = [])
{ {
$message = $this->createMessage(); $message = $this->createMessage();
if ($view !== null) { if ($view === null) {
return $message;
}
if (!array_key_exists('message', $params)) {
$params['message'] = $message; $params['message'] = $message;
if (is_array($view)) { }
if (isset($view['html'])) {
$html = $this->render($view['html'], $params, $this->htmlLayout); $this->_message = $message;
}
if (isset($view['text'])) { if (is_array($view)) {
$text = $this->render($view['text'], $params, $this->textLayout); if (isset($view['html'])) {
} $html = $this->render($view['html'], $params, $this->htmlLayout);
} else {
$html = $this->render($view, $params, $this->htmlLayout);
} }
if (isset($html)) { if (isset($view['text'])) {
$message->setHtmlBody($html); $text = $this->render($view['text'], $params, $this->textLayout);
} }
if (isset($text)) { } else {
$message->setTextBody($text); $html = $this->render($view, $params, $this->htmlLayout);
} elseif (isset($html)) { }
if (preg_match('|<body[^>]*>(.*?)</body>|is', $html, $match)) {
$html = $match[1];
} $this->_message = null;
$html = preg_replace('|<style[^>]*>(.*?)</style>|is', '', $html);
$message->setTextBody(strip_tags($html)); if (isset($html)) {
$message->setHtmlBody($html);
}
if (isset($text)) {
$message->setTextBody($text);
} elseif (isset($html)) {
if (preg_match('|<body[^>]*>(.*?)</body>|is', $html, $match)) {
$html = $match[1];
} }
$html = preg_replace('|<style[^>]*>(.*?)</style>|is', '', $html);
$message->setTextBody(strip_tags($html));
} }
return $message; return $message;
} }
...@@ -277,7 +290,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont ...@@ -277,7 +290,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont
{ {
$output = $this->getView()->render($view, $params, $this); $output = $this->getView()->render($view, $params, $this);
if ($layout !== false) { if ($layout !== false) {
return $this->getView()->render($layout, ['content' => $output], $this); return $this->getView()->render($layout, ['content' => $output, 'message' => $this->_message], $this);
} else { } else {
return $output; return $output;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment