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 10, 2024
1 parent be1f470 commit 5308f50
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
116 changes: 115 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,118 @@ to the "require" section of your composer.json.
Usage
-----

This extension
This extension provides integration of [Symfony Mailer](https://symfony.com/doc/current/mailer.html) in the Yii1 application.
Application configuration example:

```php
<?php

return [
'components' => [
'mailer' => [
'class' => yii1tech\mailer\Mailer,
'dsn' => 'smtp://user:[email protected]:25',
],
],
// ...
];
```

Usage example:

```php
<?php

use Symfony\Component\Mime\Email;

$email = (new Email())
->addFrom('[email protected]')
->addTo('[email protected]')
->subject('Greetings')
->text('Welcome to our application')
->html('<h1>Welcome to our application</h1>');

Yii::app()->mailer->send($email);
```


### Configuring Emails Globally <span id="configuring-emails-globally"></span>

Application configuration example:

```php
<?php

return [
'components' => [
'mailer' => [
'class' => yii1tech\mailer\Mailer,
'defaultHeaders' => [
'From' => 'My Application<noreply@example.com>',
'Bcc' => '[email protected]',
'X-Custom-Header' => 'foobar',
],
// ...
],
],
// ...
];
```


### Template rendering <span id="template-rendering"></span>

```php
<?php

use yii1tech\mailer\TemplatedEmail;

$email = (new TemplatedEmail())
->addFrom('[email protected]')
->addTo('[email protected]')
->subject('Greetings')
->textTemplate('greetings-text')
->htmlTemplate('greetings-html')
->context([
'name' => 'John Doe',
]);

Yii::app()->mailer->send($email);
```


### Writing unit tests <span id="writing-unit-tests"></span>

```php
<?php

return [
'components' => [
'mailer' => [
'class' => yii1tech\mailer\Mailer,
'dsn' => 'array://',
// ...
],
],
// ...
];
```

```php
<?php

class MailTest extends TestCase
{
public function testMail(): void
{
// code under test here

/** @var \Symfony\Component\Mime\Email $sentMessage */
$sentMessage = Yii::app()->mailer->getTransport()->getLastSentMessage();

$this->assetNotNull($sentMessage);
$this->assertSame('[email protected]', $sentMessage->getTo()[0]->getAddress());
// ...
}
}
```
4 changes: 2 additions & 2 deletions src/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class Mailer extends CApplicationComponent
*
* ```
* [
* 'From' => 'My App<[email protected]>',
* 'Bcc' => '[email protected]',
* 'From' => 'My Application<[email protected]>',
* 'Bcc' => 'test-via-[email protected]',
* 'X-Custom-Header' => 'foobar',
* ]
* ```
Expand Down

0 comments on commit 5308f50

Please sign in to comment.