Send mail using YiiMail extension

Introduction ΒΆ

Let me show a simpe example to send mail using YiiMail extension in 5 steps. It also includes any template view if required along with the content.

Step1: Download the extension from path 'http://code.google.com/p/yii-mail/downloads/list' .

Step 2: Place the extracted folder inside the extensions folder.

Step 3: Modify the config file main.php like below,

'mail' => array(
				'class' => 'ext.yii-mail.YiiMail',
				'transportType'=>'smtp',
				'transportOptions'=>array(
						'host'=>'<hostanme>',
						'username'=>'<username>',
						'password'=>'<password>',
						'port'=>'25',						
				),
				'viewPath' => 'application.views.mail',				
		),

Hint: The view path points to '/protected/views/mail'.

Step 4: Import the extension in the main.php file .

'ext.yii-mail.YiiMailMessage',

Step 5: Controller function to send the mail.

public function SendMail()
	{	
		$message            = new YiiMailMessage;
           //this points to the file test.php inside the view path
		$message->view = "test";
		$sid                 = 1;
		$criteria            = new CDbCriteria();
		$criteria->condition = "studentID=".$sid."";			
		$studModel1 	     = Student::model()->findByPk($sid);		
		$params              = array('myMail'=>$studModel1);
		$message->subject    = 'My TestSubject';
		$message->setBody($params, 'text/html');				
		$message->addTo('yourmail@domain.com');
		$message->from = 'admin@domain .com';	
		Yii::app()->mail->send($message);		
	}

 test.php file
  
<html>
		<head>
		</head>
		<body>
			Dear <?php 
			 echo $myMail->studentName;
			 ?>
 				<br>This is a test mail.
		</body>
	   </html>

That's it. Run the code and mail is sent.

5 0
18 followers
Viewed: 79 279 times
Version: 1.1
Category: Tutorials
Tags: YiiMail
Written by: RKA
Last updated by: RKA
Created on: Mar 19, 2013
Last updated: 5 years ago
Update Article

Revisions

View all history