Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed May 9, 2024
1 parent 324ba56 commit d046f9d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
51 changes: 47 additions & 4 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@
use yii1tech\mailer\transport\ArrayTransport;

/**
* Mailer allows email sending.
*
* Application configuration example:
*
* ```
* return [
* 'components' => [
* 'mailer' => [
* 'class' => yii1tech\mailer\Mailer,
* 'dsn' => 'smtp://user:[email protected]:25',
* 'view' => [
* 'layout' => 'default-layout',
* ],
* ],
* ],
* // ...
* ];
* ```
*
* Usage example:
*
* ```
* use Symfony\Component\Mime\Email;
*
* $email = (new Email())
* ->addFrom('[email protected]')
* ->addTo('[email protected]')
* ->subject('Test subject')
* ->text('Test body');
*
* Yii::app()->mailer->send($email);
* ```
*
* @see https://symfony.com/doc/current/mailer.html
*
* @author Paul Klimov <[email protected]>
Expand Down Expand Up @@ -57,10 +90,15 @@ class Mailer extends CApplicationComponent
/**
* @var \yii1tech\mailer\View|array view instance or its array configuration.
*/
private $_view = [
'class' => View::class,
];
private $_view = [];

/**
* Sends the given message.
*
* @param \Symfony\Component\Mime\RawMessage|\Symfony\Component\Mime\Email|\yii1tech\mailer\TemplatedEmailContract $message message to be sent.
* @param \Symfony\Component\Mailer\Envelope|null $envelope envelope instance.
* @return void
*/
public function send(RawMessage $message, ?Envelope $envelope = null): void
{
foreach ($this->defaultHeaders as $name => $value) {
Expand Down Expand Up @@ -160,7 +198,12 @@ protected function createTransport($config): TransportInterface
public function getView()
{
if (!is_object($this->_view)) {
$this->_view = Yii::createComponent($this->_view);
$config = $this->_view;
if (is_array($config) && !isset($config['class'])) {
$config['class'] = View::class;
}

$this->_view = Yii::createComponent($config);
}

return $this->_view;
Expand Down
15 changes: 15 additions & 0 deletions src/TemplatedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
/**
* TemplatedEmail allows specification of the email body parts as a rendering of the templates.
*
* Usage example:
*
* ```
* $email = (new TemplatedEmail())
* ->addTo('[email protected]')
* ->subject('Greetings')
* ->textTemplate('greetings-text')
* ->htmlTemplate('greetings-html')
* ->context([
* 'name' => 'John Doe',
* ]);
*
* Yii::app()->mailer->send($email);
* ```
*
* @see \yii1tech\mailer\View
*
* @author Paul Klimov <[email protected]>
Expand Down
7 changes: 5 additions & 2 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
/**
* View is a email template view renderer.
*
* Instance of this class will be available inside the view templates as `$this` variable.
*
* This class uses the custom view renderer, if it is set at the application level.
*
* @property string $viewPath the root directory of view files. Defaults to 'views/mail' under the application base path.
* @property \IViewRenderer|\CViewRenderer|array|string|null|false $viewRenderer view renderer or its array configuration.
*
Expand Down Expand Up @@ -59,7 +63,7 @@ public function setViewPath(string $viewPath): self

/**
* Returns the view renderer - a component whose wants to replace the default view rendering logic.
* By default the application 'viewRenderer' component will be used, if it has been set.
* By default, the application 'viewRenderer' component will be used, if it has been set.
*
* @return \IViewRenderer|\CViewRenderer|null|false view renderer.
*/
Expand Down Expand Up @@ -205,7 +209,6 @@ public function render(string $view, ?array $data = null, ?string $locale = null
* @param array|null $data data to be extracted into PHP variables and made available to the view script.
* @param bool $return whether the rendering result should be returned instead of being sent to output.
* @return string|null the rendering result. `Null` if the rendering result is not required.
* @throws \CException
*/
public function renderPartial(string $view, ?array $data = null, bool $return = false)
{
Expand Down
1 change: 0 additions & 1 deletion tests/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public function testSetupView(): void
$mailer = Yii::createComponent([
'class' => Mailer::class,
'view' => [
'class' => View::class,
'layout' => 'test-layout',
],
]);
Expand Down

0 comments on commit d046f9d

Please sign in to comment.