switching to email service
This commit is contained in:
parent
233a8a6d10
commit
61085a1362
23
common/models/email/Email.php
Normal file
23
common/models/email/Email.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace common\models\email;
|
||||||
|
|
||||||
|
class Email
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public string $sendTo;
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public string $subject;
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public array $mailLayout;
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public array $params;
|
||||||
|
}
|
20
common/models/email/RegistrationEmail.php
Normal file
20
common/models/email/RegistrationEmail.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace common\models\email;
|
||||||
|
|
||||||
|
use common\models\User;
|
||||||
|
use Yii;
|
||||||
|
|
||||||
|
class RegistrationEmail extends Email
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
*/
|
||||||
|
public function __construct(User $user)
|
||||||
|
{
|
||||||
|
$this->sendTo = $user->email;
|
||||||
|
$this->subject = 'Account registration at ' . Yii::$app->name;
|
||||||
|
$this->mailLayout = ['html' => 'signup-html', 'text' => 'signup-text'];
|
||||||
|
$this->params = ['user' => $user];
|
||||||
|
}
|
||||||
|
}
|
33
common/services/EmailService.php
Normal file
33
common/services/EmailService.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace common\services;
|
||||||
|
|
||||||
|
use common\models\email\Email;
|
||||||
|
use Yii;
|
||||||
|
|
||||||
|
class EmailService
|
||||||
|
{
|
||||||
|
private array $sendFrom;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->sendFrom = [Yii::$app->params['senderEmail'] => Yii::$app->name . ' robot'];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Email $email
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function sendEmail(Email $email): bool
|
||||||
|
{
|
||||||
|
return Yii::$app->mailer->compose(
|
||||||
|
$email->mailLayout,
|
||||||
|
$email->params,
|
||||||
|
)
|
||||||
|
->setFrom($this->sendFrom)
|
||||||
|
->setTo($email->sendTo)
|
||||||
|
->setSubject($email->subject)
|
||||||
|
->send();
|
||||||
|
}
|
||||||
|
}
|
@ -60,28 +60,6 @@ class SignupForm extends Model
|
|||||||
$authorRole = $auth->getRole('user');
|
$authorRole = $auth->getRole('user');
|
||||||
$auth->assign($authorRole, $user->id);
|
$auth->assign($authorRole, $user->id);
|
||||||
|
|
||||||
if ($user->save()) {
|
return $user->save() ? $user : null;
|
||||||
$this->sendEmail($user);
|
|
||||||
return $user;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends an email with a link, for resetting the password.
|
|
||||||
*
|
|
||||||
* @return bool whether the email was send
|
|
||||||
*/
|
|
||||||
private function sendEmail(User $user)
|
|
||||||
{
|
|
||||||
return Yii::$app->mailer->compose(
|
|
||||||
['html' => 'signup-html', 'text' => 'signup-text'],
|
|
||||||
['user' => $this]
|
|
||||||
)
|
|
||||||
->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->name . ' robot'])
|
|
||||||
->setTo($user->email)
|
|
||||||
->setSubject('Account registration at ' . Yii::$app->name)
|
|
||||||
->send();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
namespace frontend\modules\api\controllers;
|
namespace frontend\modules\api\controllers;
|
||||||
|
|
||||||
use common\classes\Debug;
|
use common\models\email\RegistrationEmail;
|
||||||
use common\models\User;
|
use common\models\User;
|
||||||
|
use common\services\EmailService;
|
||||||
use frontend\models\SignupForm;
|
use frontend\models\SignupForm;
|
||||||
use Yii;
|
use Yii;
|
||||||
|
|
||||||
@ -16,6 +17,14 @@ class RegisterController extends ApiController
|
|||||||
return $newBehavior;
|
return $newBehavior;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private EmailService $emailService;
|
||||||
|
|
||||||
|
public function __construct($id, $module, EmailService $emailService, $config = [])
|
||||||
|
{
|
||||||
|
$this->emailService = $emailService;
|
||||||
|
parent::__construct($id, $module, $config);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @OA\Post(path="/register/sign-up",
|
* @OA\Post(path="/register/sign-up",
|
||||||
@ -60,6 +69,7 @@ class RegisterController extends ApiController
|
|||||||
if ($model->load(Yii::$app->getRequest()->getBodyParams(), '')) {
|
if ($model->load(Yii::$app->getRequest()->getBodyParams(), '')) {
|
||||||
/** @var User $user */
|
/** @var User $user */
|
||||||
if ($user = $model->signup()) {
|
if ($user = $model->signup()) {
|
||||||
|
$this->emailService->sendEmail(new RegistrationEmail($user));
|
||||||
return [
|
return [
|
||||||
'id' => $user->id,
|
'id' => $user->id,
|
||||||
];
|
];
|
||||||
|
Loading…
Reference in New Issue
Block a user