From eaa6bf967fc5f8ceb11b28647181af2affcc4a0b Mon Sep 17 00:00:00 2001 From: bscheshirwork Date: Tue, 23 Oct 2018 12:05:53 +0300 Subject: [PATCH] add Closure support for fromEmail (#274) --- CHANGELOG.md | 1 + docs/installation/mailer.md | 51 ++++++++++++++++++++++++++++++-- src/User/Factory/MailFactory.php | 5 +++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdfd888..fe940d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/installation/mailer.md b/docs/installation/mailer.md index b9db47e..dd91bb3 100644 --- a/docs/installation/mailer.md +++ b/docs/installation/mailer.md @@ -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 diff --git a/src/User/Factory/MailFactory.php b/src/User/Factory/MailFactory.php index 13fd3e5..ba81bca 100644 --- a/src/User/Factory/MailFactory.php +++ b/src/User/Factory/MailFactory.php @@ -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,