Skip to content

Commit

Permalink
Fixes yiisoft#3992
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Jul 1, 2014
1 parent bced469 commit a2a6028
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
6 changes: 3 additions & 3 deletions apps/advanced/common/mail/layouts/html.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
use yii\helpers\Html;

/* @var $this \yii\web\View */
/* @var $content string */

/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message bing composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Expand Down
6 changes: 3 additions & 3 deletions apps/basic/mail/layouts/html.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
use yii\helpers\Html;

/* @var $this \yii\web\View */
/* @var $content string */

/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message bing composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Expand Down
3 changes: 2 additions & 1 deletion docs/guide/tutorial-mailing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
?>
<?php $this->beginPage() ?>
Expand Down Expand Up @@ -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.
Then you need to implement all abstract methods to build you solution.
2 changes: 2 additions & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
55 changes: 34 additions & 21 deletions framework/mail/BaseMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ protected function createView(array $config)
return Yii::createObject($config);
}

private $_message;

/**
* Creates a new message instance and optionally composes its body content via view rendering.
*
Expand All @@ -167,30 +169,41 @@ protected function createView(array $config)
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('|<body[^>]*>(.*?)</body>|is', $html, $match)) {
$html = $match[1];
}
$html = preg_replace('|<style[^>]*>(.*?)</style>|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('|<body[^>]*>(.*?)</body>|is', $html, $match)) {
$html = $match[1];
}
$html = preg_replace('|<style[^>]*>(.*?)</style>|is', '', $html);
$message->setTextBody(strip_tags($html));
}
return $message;
}
Expand Down Expand Up @@ -277,7 +290,7 @@ public function render($view, $params = [], $layout = false)
{
$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;
}
Expand Down

0 comments on commit a2a6028

Please sign in to comment.