diff --git a/apps/advanced/common/mail/layouts/html.php b/apps/advanced/common/mail/layouts/html.php index 6c083957db..c2fb7a6402 100644 --- a/apps/advanced/common/mail/layouts/html.php +++ b/apps/advanced/common/mail/layouts/html.php @@ -1,9 +1,9 @@ beginPage() ?> diff --git a/apps/basic/mail/layouts/html.php b/apps/basic/mail/layouts/html.php index 6c083957db..c2fb7a6402 100644 --- a/apps/basic/mail/layouts/html.php +++ b/apps/basic/mail/layouts/html.php @@ -1,9 +1,9 @@ beginPage() ?> diff --git a/docs/guide/tutorial-mailing.md b/docs/guide/tutorial-mailing.md index 11697166cb..b26e8b2366 100644 --- a/docs/guide/tutorial-mailing.md +++ b/docs/guide/tutorial-mailing.md @@ -141,6 +141,7 @@ Layout can be used to setup mail CSS styles or other shared content: use yii\helpers\Html; /* @var $this \yii\web\View view component instance */ +/* @var $message \yii\mail\MessageInterface the message bing composed */ /* @var $content string main view render result */ ?> beginPage() ?> @@ -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 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. -Then you need to implement all abstract methods to build you solution. \ No newline at end of file +Then you need to implement all abstract methods to build you solution. diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 7584a2bfed..5d15a8478b 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -121,6 +121,7 @@ Yii Framework 2 Change Log - Removed character maps for non-latin languages. - Improved overall slug results. - 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 #4072: `\yii\rbac\PhpManager` adjustments (samdark) - 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 - 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 #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: 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) diff --git a/framework/mail/BaseMailer.php b/framework/mail/BaseMailer.php index d34921e135..ea20069ca8 100644 --- a/framework/mail/BaseMailer.php +++ b/framework/mail/BaseMailer.php @@ -144,6 +144,8 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont return Yii::createObject($config); } + private $_message; + /** * 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 public function compose($view = null, array $params = []) { $message = $this->createMessage(); - if ($view !== null) { + if ($view === null) { + return $message; + } + + if (!array_key_exists('message', $params)) { $params['message'] = $message; - if (is_array($view)) { - if (isset($view['html'])) { - $html = $this->render($view['html'], $params, $this->htmlLayout); - } - if (isset($view['text'])) { - $text = $this->render($view['text'], $params, $this->textLayout); - } - } else { - $html = $this->render($view, $params, $this->htmlLayout); + } + + $this->_message = $message; + + if (is_array($view)) { + if (isset($view['html'])) { + $html = $this->render($view['html'], $params, $this->htmlLayout); } - if (isset($html)) { - $message->setHtmlBody($html); + if (isset($view['text'])) { + $text = $this->render($view['text'], $params, $this->textLayout); } - if (isset($text)) { - $message->setTextBody($text); - } elseif (isset($html)) { - if (preg_match('|]*>(.*?)|is', $html, $match)) { - $html = $match[1]; - } - $html = preg_replace('|]*>(.*?)|is', '', $html); - $message->setTextBody(strip_tags($html)); + } else { + $html = $this->render($view, $params, $this->htmlLayout); + } + + + $this->_message = null; + + if (isset($html)) { + $message->setHtmlBody($html); + } + if (isset($text)) { + $message->setTextBody($text); + } elseif (isset($html)) { + if (preg_match('|]*>(.*?)|is', $html, $match)) { + $html = $match[1]; } + $html = preg_replace('|]*>(.*?)|is', '', $html); + $message->setTextBody(strip_tags($html)); } return $message; } @@ -277,7 +290,7 @@ abstract class BaseMailer extends Component implements MailerInterface, ViewCont { $output = $this->getView()->render($view, $params, $this); if ($layout !== false) { - return $this->getView()->render($layout, ['content' => $output], $this); + return $this->getView()->render($layout, ['content' => $output, 'message' => $this->_message], $this); } else { return $output; }