switching to email service
This commit is contained in:
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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user