From e3292c959accd23eba9eb07b4194b6b4e41553ee Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 27 Nov 2013 03:48:20 +0400 Subject: [PATCH] Advanced application template now uses mailer component instead of mail() function --- apps/advanced/common/config/params.php | 1 + apps/advanced/common/mails/passwordResetToken.php | 16 ++++++++++++++++ apps/advanced/frontend/controllers/SiteController.php | 16 +++++----------- .../frontend/views/emails/passwordResetToken.php | 16 ---------------- 4 files changed, 22 insertions(+), 27 deletions(-) create mode 100644 apps/advanced/common/mails/passwordResetToken.php delete mode 100644 apps/advanced/frontend/views/emails/passwordResetToken.php diff --git a/apps/advanced/common/config/params.php b/apps/advanced/common/config/params.php index dd01b75..c378364 100644 --- a/apps/advanced/common/config/params.php +++ b/apps/advanced/common/config/params.php @@ -15,6 +15,7 @@ return [ 'components.mail' => [ 'class' => 'yii\swiftmailer\Mailer', + 'viewPath' => '@common/mails', ], 'components.db' => [ diff --git a/apps/advanced/common/mails/passwordResetToken.php b/apps/advanced/common/mails/passwordResetToken.php new file mode 100644 index 0000000..b617bd9 --- /dev/null +++ b/apps/advanced/common/mails/passwordResetToken.php @@ -0,0 +1,16 @@ +urlManager->createAbsoluteUrl('site/reset-password', ['token' => $user->password_reset_token]); +?> + +Hello username) ?>, + +Follow the link below to reset your password: + + diff --git a/apps/advanced/frontend/controllers/SiteController.php b/apps/advanced/frontend/controllers/SiteController.php index 184d16c..d617d7e 100644 --- a/apps/advanced/frontend/controllers/SiteController.php +++ b/apps/advanced/frontend/controllers/SiteController.php @@ -159,17 +159,11 @@ class SiteController extends Controller $user->password_reset_token = Security::generateRandomKey(); if ($user->save(false)) { - // todo: refactor it with mail component. pay attention to the arrangement of mail view files - $fromEmail = \Yii::$app->params['supportEmail']; - $name = '=?UTF-8?B?' . base64_encode(\Yii::$app->name . ' robot') . '?='; - $subject = '=?UTF-8?B?' . base64_encode('Password reset for ' . \Yii::$app->name) . '?='; - $body = $this->renderPartial('/emails/passwordResetToken', [ - 'user' => $user, - ]); - $headers = "From: $name <{$fromEmail}>\r\n" . - "MIME-Version: 1.0\r\n" . - "Content-type: text/plain; charset=UTF-8"; - return mail($email, $subject, $body, $headers); + return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user]) + ->from([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot']) + ->to($email) + ->subject('Password reset for ' . \Yii::$app->name) + ->send(); } return false; diff --git a/apps/advanced/frontend/views/emails/passwordResetToken.php b/apps/advanced/frontend/views/emails/passwordResetToken.php deleted file mode 100644 index b617bd9..0000000 --- a/apps/advanced/frontend/views/emails/passwordResetToken.php +++ /dev/null @@ -1,16 +0,0 @@ -urlManager->createAbsoluteUrl('site/reset-password', ['token' => $user->password_reset_token]); -?> - -Hello username) ?>, - -Follow the link below to reset your password: - -