Mailer
Mailer for Yii2, (*1)
Installation
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist tecsvit/yii2-mailer "*"
or (dev), (*4)
composer require --prefer-dist tecsvit/yii2-mailer "@dev"
or add, (*5)
"tecsvit/yii2-mailer": "*"
to the require section of your composer.json
file., (*6)
Usage
Once the extension is installed, simply use it in your code by :, (*7)
...
'components' => [
...
'mailer' => [
'class' => '\tecsvit\mailer\components\Mailer',
'useFileTransport' => false,
'defaultTemplates' => [
'YOUR_NAME_TEMPLATE' => [
'subject' => 'Your_mail_subject',
'template' => 'Your_template_filename',
'senderMail' => 'Sender_Email',
'senderName' => 'Sender_Name',
],
],
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'your_host',
'username' => 'your_username',
'password' => 'your_pass',
'port' => 'your_port',
'encryption' => 'your_encryption',
],
],
...
Example:, (*8)
...
'components' => [
...
'mailer' => [
'class' => '\tecsvit\mailer\components\Mailer',
'useFileTransport' => false,
'defaultTemplates' => [
'confirmPassword' => [
'subject' => 'Hi',
'template' => 'confirmPassword.php',
'senderMail' => 'admin@admin.com',
'senderName' => 'Admin',
],
'subscribe' => [
'subject' => 'Hi2',
'template' => 'subscribe.php',
'senderMail' => 'subscribe@admin.com',
'senderName' => 'Manager',
],
],
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'user@gmail.com',
'password' => '4565456464',
'port' => '587',
'encryption' => 'tls',
],
],
...
Create template(s) in directory ../app/mail/ (example):, (*9)
confirmPassword.php, (*10)
<strong>Confirm Your Mail</strong>
<div>
For confirm your mail click on link: <a href="http://site.com/user/confirm-email?hash=<?= $hash ?>">click</a>
</div>
In your code, (*11)
$toMail = 'mail-your-friend@gmail.com';
$data = ['hash' => $hash];
/** @var \tecsvit\mailer\components\Mailer $mailer */
$mailer = Yii::$app->mailer;
$mailer->sendMail($toMail, $data, 'confirmPassword');