Skip to content

Commit d046f9d

Browse files
committed
add docs
1 parent 324ba56 commit d046f9d

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

src/Mailer.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@
1212
use yii1tech\mailer\transport\ArrayTransport;
1313

1414
/**
15+
* Mailer allows email sending.
16+
*
17+
* Application configuration example:
18+
*
19+
* ```
20+
* return [
21+
* 'components' => [
22+
* 'mailer' => [
23+
* 'class' => yii1tech\mailer\Mailer,
24+
* 'dsn' => 'smtp://user:[email protected]:25',
25+
* 'view' => [
26+
* 'layout' => 'default-layout',
27+
* ],
28+
* ],
29+
* ],
30+
* // ...
31+
* ];
32+
* ```
33+
*
34+
* Usage example:
35+
*
36+
* ```
37+
* use Symfony\Component\Mime\Email;
38+
*
39+
* $email = (new Email())
40+
* ->addFrom('[email protected]')
41+
* ->addTo('[email protected]')
42+
* ->subject('Test subject')
43+
* ->text('Test body');
44+
*
45+
* Yii::app()->mailer->send($email);
46+
* ```
47+
*
1548
* @see https://symfony.com/doc/current/mailer.html
1649
*
1750
* @author Paul Klimov <[email protected]>
@@ -57,10 +90,15 @@ class Mailer extends CApplicationComponent
5790
/**
5891
* @var \yii1tech\mailer\View|array view instance or its array configuration.
5992
*/
60-
private $_view = [
61-
'class' => View::class,
62-
];
93+
private $_view = [];
6394

95+
/**
96+
* Sends the given message.
97+
*
98+
* @param \Symfony\Component\Mime\RawMessage|\Symfony\Component\Mime\Email|\yii1tech\mailer\TemplatedEmailContract $message message to be sent.
99+
* @param \Symfony\Component\Mailer\Envelope|null $envelope envelope instance.
100+
* @return void
101+
*/
64102
public function send(RawMessage $message, ?Envelope $envelope = null): void
65103
{
66104
foreach ($this->defaultHeaders as $name => $value) {
@@ -160,7 +198,12 @@ protected function createTransport($config): TransportInterface
160198
public function getView()
161199
{
162200
if (!is_object($this->_view)) {
163-
$this->_view = Yii::createComponent($this->_view);
201+
$config = $this->_view;
202+
if (is_array($config) && !isset($config['class'])) {
203+
$config['class'] = View::class;
204+
}
205+
206+
$this->_view = Yii::createComponent($config);
164207
}
165208

166209
return $this->_view;

src/TemplatedEmail.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
/**
88
* TemplatedEmail allows specification of the email body parts as a rendering of the templates.
99
*
10+
* Usage example:
11+
*
12+
* ```
13+
* $email = (new TemplatedEmail())
14+
* ->addTo('[email protected]')
15+
* ->subject('Greetings')
16+
* ->textTemplate('greetings-text')
17+
* ->htmlTemplate('greetings-html')
18+
* ->context([
19+
* 'name' => 'John Doe',
20+
* ]);
21+
*
22+
* Yii::app()->mailer->send($email);
23+
* ```
24+
*
1025
* @see \yii1tech\mailer\View
1126
*
1227
* @author Paul Klimov <[email protected]>

src/View.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
/**
99
* View is a email template view renderer.
1010
*
11+
* Instance of this class will be available inside the view templates as `$this` variable.
12+
*
13+
* This class uses the custom view renderer, if it is set at the application level.
14+
*
1115
* @property string $viewPath the root directory of view files. Defaults to 'views/mail' under the application base path.
1216
* @property \IViewRenderer|\CViewRenderer|array|string|null|false $viewRenderer view renderer or its array configuration.
1317
*
@@ -59,7 +63,7 @@ public function setViewPath(string $viewPath): self
5963

6064
/**
6165
* Returns the view renderer - a component whose wants to replace the default view rendering logic.
62-
* By default the application 'viewRenderer' component will be used, if it has been set.
66+
* By default, the application 'viewRenderer' component will be used, if it has been set.
6367
*
6468
* @return \IViewRenderer|\CViewRenderer|null|false view renderer.
6569
*/
@@ -205,7 +209,6 @@ public function render(string $view, ?array $data = null, ?string $locale = null
205209
* @param array|null $data data to be extracted into PHP variables and made available to the view script.
206210
* @param bool $return whether the rendering result should be returned instead of being sent to output.
207211
* @return string|null the rendering result. `Null` if the rendering result is not required.
208-
* @throws \CException
209212
*/
210213
public function renderPartial(string $view, ?array $data = null, bool $return = false)
211214
{

tests/MailerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ public function testSetupView(): void
111111
$mailer = Yii::createComponent([
112112
'class' => Mailer::class,
113113
'view' => [
114-
'class' => View::class,
115114
'layout' => 'test-layout',
116115
],
117116
]);

0 commit comments

Comments
 (0)