Skip to main content

Send email in Yii framework

Yii PHP framework has an extension is swiftmailer. With that extension, you can send email from your website.
  1. Open config/web.php file. Find 'components' => [ and paste this codes below it:
      1. 'mail' => [
            'class' => 'yii\swiftmailer\Mailer',
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.gmail.com',  
                'username' => [email protected]',
                'password' => 'your.password',
                 'port' => '587',
                 'encryption' => 'tls',
               ],
        ],
  2. Paste below code into where you want to send email:
    1. Yii::$app->mail->compose()
      ->setFrom('[email protected]')
      ->setTo(['[email protected]'=>'name1', '[email protected]'=>'name2'])
      ->setSubject('Hello')
      ->setHtmlBody('<b>Hello,</b><br/>
      This email send from Yii website.
      Regards!')
      ->send();
       
Note: If you have another mail server, change information at step 1 instead of Gmail.
That's all. Wish succeed!

Comments

Popular posts from this blog