Yii framework : Send phpmailer , SMTP


Reference Link : Download extension : smtp-mail

Place This Code in protected/config/main.php file

'components'=>array(
'Smtpmail'=>array(
'class'=>'application.extensions.smtpmail.PHPMailer',
'Host'=>"mail.yourdomain.com",
'Username'=>'test@yourdomain.com',
'Password'=>'test',
'Mailer'=>'smtp',
'Port'=>26,
'SMTPAuth'=>true,
),
),

Place This Code in protected/components/Controller.php file

public function mailsend($to,$from,$from_name,$subject,$message,$cc=array(),$attachment=array())
{
$mail = Yii::app()->Smtpmail;
$mail->SetFrom($from,$from_name);
$mail->Subject = $subject;
$mail->MsgHTML($this->mailTemplate($message));
$mail->AddAddress($to, "");

// Add CC
if(!empty($cc)){
foreach($cc as $email){
$mail->AddCC($email);
}
}

// Add Attchments
if(!empty($attachment)){
foreach($attachment as $attach){
$mail->AddAttachment($attach);
}
}

if(!$mail->Send()) {
return false; // Fail echo "Mailer Error: " . $mail->ErrorInfo;
}else {
return true; // Success
}
}

Call this function from your controller like this

$this->mailsend($to,$from,$from_name,$subject,$message,$cc,$attachment);

Advertisements

Set Deactive Data Row color to red


Go to protected/views/city(whatever your view folder)/admin.php add below highligheted code.

And Create Class with “red”

.red {
background-color:#F8CFCC !important;
}


$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'city-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'rowCssClassExpression' => '
( $row%2 ? $this->rowCssClass[1] : $this->rowCssClass[0] ) .
( $data->is_active==0 ? null : " red" )'
,
'columns'=>array(
'city_name',
array( 'name'=>'name', 'value'=>'$data->state->name' ),
array( 'name'=>'c_name', 'value'=>'$data->country->c_name' ),

array(
'name' =>'is_active',
'value'=> '$data->is_active == 0 ? "Active":"Deactive"'
),

array(
'class'=>'CButtonColumn'
),
),
))

Yii framework multiselect dropdown


view/_form

$location_list=MtLocation::model()->findAll(array('order'=>'created_on'));
$location_list_arr=CHtml::listData($location_list,'mt_location_id','location_name');

echo CHtml::dropDownList('MtGetintouch[mt_location_id][]', explode(",",$model->mt_location_id), $location_list_arr,array('empty'=>'Select Locaction Name','multiple'=>'multiple'));

Getintouch Controller
Both in actionUpdate / actionCreate methods

$_POST['MtGetintouch']['mt_location_id'] = implode(",",$_POST['MtGetintouch']['mt_location_id']);

Display Comma Separated locations value in Gridview

public function getLocations($id)
{
$ids= explode(",",$id);
foreach($ids as $key=>$val){
$ids[$key]=(int)$val;
}
$data = Yii::app()->db->createCommand()->select ('GROUP_CONCAT(location_name) as locations')->from('mt_location')->where(array('in','mt_location_id',$ids))->queryRow();
return $data["locations"];

}

Yii framework – call controller function from view


public function getLocations($id)
{
$ids= explode(“,”,$id);
foreach($ids as $key=>$val){
$ids[$key]=(int)$val;
}
$data = Yii::app()->db->createCommand()->select (‘GROUP_CONCAT(location_name) as locations’)->from(‘mt_location’)->where(array(‘in’,’mt_location_id’,$ids))->queryRow();
return $data[“locations”];

}

in view file

array(
‘name’=>’mt_location_id’,
‘value’=>’MtGetintouchController::getLocations($data->mt_location_id)’,
),