SENDING EMAIL IN YII2

 


Configure your application by adding this to common/config/main.php

'components' => [

'mailer' => [
 'class' => 'yii\swiftmailer\Mailer',
 'viewPath' => '@app/mailer',
 'useFileTransport' => false,
 'transport' => [
 'class' => 'Swift_SmtpTransport',
 'host' => 'smtp.gmail.com',
 'username' => 'osa.system.2018@gmail.com',
 'password' => 'your_password',
 'port' => '587',
 'encryption' => 'tls',
 ],
 ],
 ],

Create a view file under folder views/mail/examapplication-response.php

You can put variables which we will pass later in the controller…

<?php
use yii\helpers\Html;

/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
 <style type="text/css">
 </style>
 <?php $this->head() ?>
</head>
<body>
 <?php $this->beginBody() ?>
 
 <title>Exam Application</title>
 </head>
 <body>

<h1>Exam Application at OSA Information System</h1>
 Dear <?= $firstname; echo' '.$lastname;?> :<br/><br/>
 Your application for an exam on <b> <?= $course; ?> </b> at <b><?= $venue; ?></b> was received. <br/>
 Kindly wait for a notification email confirming its approval. Should you wish to request for change of venue, kindly use the request for change of venue form. <br/>
 <br/>For questions or other concerns, you may contact us at osa.system.2018@gmail.com. <br/>
 <br/>Thank you very much!<br/><br/>
 

<?php $this->endBody() ?> </body> </html> <?php $this->endPage() ?>

Add this to your controller

$venue = Listofvenues::findOne($model->listofvenues_id);

Yii::$app->mailer
 ->compose('//mail\examapplication-response', [
 'user' => Yii::$app->user->identity,
 'course' => $model->coursesWithProctoredExam,
 'lastname' => $model->studentLastName,
 'firstname' => $model->studentFirstName,
 'date' => $model->examDate,
 'venue'=>$venue->name,
 ])
 ->setFrom('osa.system.2018@gmail.com')
 ->setTo($student->emailAddress)
 ->setSubject('Exam Application at OSA System')
 ->setTextBody('Thank you for applying for an exam. Your application has been received.')
 ->send();

Post a Comment

0 Comments