PHP Tips & Tricks YII2

How to Use the SwiftMailer in Yii2

How to Use the SwiftMailer in Yii2 July 27, 2017Leave a comment

Hello World, I am Chandra Kumar from New Delhi, India. I am professionally developer, programmer and blogger. I am the founder of WebShouters.com.

Yii2.0 Framework

In this tutorial, you will go through how to use swiftMailer in yii2 application. Sending emails from yii2 application is not a tedious task, in fact we just need to edit/add the config options in your config file.

Make sure you have initialized your application in production environment to send emails from your application, else it will be written in to the mailoutput – folder.

In the components’s section of your common/main-local.php

'mail' => [
	'class' => 'yii\swiftmailer\Mailer',
	'viewPath' => '@backend/mail',
	'useFileTransport' => false,//set this property to false to send mails to real email addresses
	//comment the following array to send mail using php's mail function
	'transport' => [
		'class' => 'Swift_SmtpTransport',
		'host' => 'smtp.gmail.com',
		'username' => '[email protected]',
		'password' => 'password',
		'port' => '587',
		'encryption' => 'tls',
					],
],
],

In your Controller

\Yii::$app->mail->compose('your_view', ['params' => $params])
    ->setFrom([\Yii::$app->params['supportEmail'] => 'Test Mail'])
    ->setTo('[email protected]')
    ->setSubject('This is a test mail ' )
    ->send();

Hope this is useful for you 🙂

How to Use the SwiftMailer in Yii2
5 (100%) 1 vote

(Visited 2 times, 1 visits today)

Hello World, I am Chandra Kumar from New Delhi, India. I am professionally developer, programmer and blogger. I am the founder of WebShouters.com.

Leave a Reply

Your email address will not be published. Required fields are marked *