Send mail to gmail account using localhost from yii2 with phpmailer

I have created contact form with fields name,email,phone,message which saves in database.

I want the contact form to send email using phpmailer through localhost in Yii2. I tried following up the link still not able to send email.

Steps taken

  1. I have downloaded phpmailer through composer.

  2. I have set-up web.php in config folder as

‘mail’ => [

        'class'            => 'zyx\phpmailer\Mailer',

        'viewPath'         => 'yii2Basic',

        'useFileTransport' => false,

        'config'           => [

            'mailer'     => 'smtp',

            'host'       => '',

            'port'       => '587',

            'smtpsecure' => 'tsl',

            'smtpauth'   => true,

            'username'   => 'mygmail',

            'password'   => '*******',


  1. Controller action

public function actionCreate()


    $model = new Contact();

    if ($model->load(Yii::$app->request->post()) ) 



         ->setFrom(['mygmail' => 'My Example Message'])

         ->setTo([$model->email => $model->name])

         ->setSubject('test subject')





        //return $this->redirect(['view', 'id' => $model->id]);

         return $this->redirect(['index']);


    } else {

        return $this->render('create', [

            'model' => $model,




still the code save to my db but does not send email to gmail.

Kindly help me where I am going wrong and what should I do.