add Closure support for fromEmail (#274)

This commit is contained in:
bscheshirwork
2018-10-23 12:05:53 +03:00
committed by Lorenzo Milesi
parent 16bd448042
commit eaa6bf967f
3 changed files with 54 additions and 3 deletions

View File

@ -21,6 +21,7 @@
- Fix #252: Delete check for unexpected property `allowPasswordRecovery` for resend email by admin (bscheshirwork)
- Fix #254: Rename `GDPR` properties to `lowerCamelCase` style (bscheshirwork)
- Fix #258: Rename `GDPR` delete action to `lowerCamelCase`/`dash` style (bscheshirwork)
- Fix #271: Add closure support for `from` email address; Change default sender to `supportEmail` (bscheshirwork)
- Fix #276: Fix missing translatable strings
## 1.1.4 - February 19, 2018

View File

@ -4,7 +4,7 @@ Mailer
The way this module sends its emails is throughout the [`Mailer`](http://www.yiiframework.com/doc-2.0/guide-tutorial-mailing.html)
component of Yii 2. Please, follow Yii 2's guidelines to set it up.
Nevertheless, you have to configure the following attribute of the module: `mailParams`. the following is its default
Nevertheless, you wish to configure the following attribute of the module: `mailParams`. the following is its default
values:
```php
@ -17,7 +17,23 @@ values:
]
```
Actually, the only thing required is the `fromEmail` value. If you look at the code of `Da\User\Factory\MailFactory.php`
Actually, the only thing required is the `fromEmail` value.
If you want to set it the same as supportEmail from your config params (like yii2-app-advanced template):
```php
...
'modules' => [
'user' => [
'class' => Da\User\Module::class,
'mailParams' => [
'fromEmail' => function() {
return [Yii::$app->params['supportEmail'] => Yii::t('app', '{0} robot', Yii::$app->name)];
}
],
],
],
...
```
If you look at the code of `Da\User\Factory\MailFactory.php`
you will easily find the reason why:
```php
@ -55,4 +71,35 @@ public function run()
}
```
> Tip: You can separate `from` by type of mailer of this module:
```php
...
'modules' => [
'user' => [
'class' => Da\User\Module::class,
'mailParams' => [
'fromEmail' =>
/**
* @param $type string The type of mail
* Da\User\Event\MailEvent::TYPE_WELCOME|Da\User\Event\MailEvent::TYPE_RECOVERY|
* Da\User\Event\MailEvent::TYPE_CONFIRM|Da\User\Event\MailEvent::TYPE_RECONFIRM
* @return array
*/
function ($type) {
switch ($type) {
case Da\User\Event\MailEvent::TYPE_WELCOME:
return [Yii::$app->params['supportEmail'] => Yii::t('app', '{0} welcome!', Yii::$app->name)];
break;
default:
return [Yii::$app->params['supportEmail'] => Yii::t('app', '{0} robot', Yii::$app->name)];
break;
}
},
],
],
],
...
```
© [2amigos](http://www.2amigos.us/) 2013-2017

View File

@ -118,7 +118,7 @@ class MailFactory
* Builds a MailerService.
*
* @param string $type
* @param string $from
* @param string|array|\Closure $from
* @param string $to
* @param string $subject
* @param string $view
@ -130,6 +130,9 @@ class MailFactory
*/
public static function makeMailerService($type, $from, $to, $subject, $view, $params = [])
{
if ($from instanceof \Closure){
$from = $from($type);
}
/** @noinspection PhpIncompatibleReturnTypeInspection */
return Yii::$container->get(
MailService::class,