- Download this mail extension here http://www.yiiframework.com/extension/mail.
- Extract the files.
- Move the vendors folder to protected/components/vendors/
- Move YiiMail.php and YiiMailMessage.php to protected/components/
- Add on protected/config/main.php
[php]
‘components’=>array(
…
‘mail’ => array(
‘class’ => ‘YiiMail’,
‘transportType’ => ‘php’,
‘transportType’ => ‘smtp’,
‘transportOptions’=>array(
‘host’=>’ghazalitajuddin.com’,
//’encryption’=>’tls’,
‘username’=>’***@ghazalitajuddin.com’,
‘password’=>’******’,
‘port’=>25,
),
‘logging’ => true,
‘dryRun’ => false
),
…
),
[/php]
- Configure in controller
[php]</pre>
$message = new YiiMailMessage;
$message->setBody($model->message);
$message->subject = $model->subject;
$message->addTo($model->to);
$message->from = Yii::app()->params[‘adminEmail’];
Yii::app()->mail->send($message);
<pre>[/php]
- Create our model
[php]
class EmailForm extends CFormModel
{
public $email;
public $to;
public $subject;
public $message;
public $from;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array(’email, to, subject, message’, ‘required’),
// email has to be a valid email address
array(’email’, ’email’),
// verifyCode needs to be entered correctly
//array(‘verifyCode’, ‘captcha’, ‘allowEmpty’=>!CCaptcha::checkRequirements()),
);
}
/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return array(
‘verifyCode’=>’Verification Code’,
);
}
}
[/php]
- Finally create our view
[php]
<?php
$this->pageTitle=Yii::app()->name . ‘ – Email Others’;
$this->breadcrumbs=array(
‘Email’,
);
?>
<h1>Email others</h1>
<?php if(Yii::app()->user->hasFlash(’email’)): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash(’email’); ?>
</div>
<?php else: ?>
<p>
If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
</p>
<div class="form">
<?php $form=$this->beginWidget(‘CActiveForm’, array(
‘id’=>’email-form’,
‘enableClientValidation’=>true,
‘clientOptions’=>array(
‘validateOnSubmit’=>true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,’email’); ?>
<?php echo $form->textField($model,’email’); ?>
<?php echo $form->error($model,’email’); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,’to’); ?>
<?php echo $form->textField($model,’to’); ?>
<?php echo $form->error($model,’to’); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,’subject’); ?>
<?php echo $form->textField($model,’subject’,array(‘size’=>60,’maxlength’=>128)); ?>
<?php echo $form->error($model,’subject’); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,’message’); ?>
<?php echo $form->textArea($model,’message’,array(‘rows’=>6, ‘cols’=>50)); ?>
<?php echo $form->error($model,’message’); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,’from’); ?>
<?php echo $form->textArea($model,’from’); ?>
<?php echo $form->error($model,’from’); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton(‘Submit’); ?>
</div>
<?php $this->endWidget(); ?>
</div><!– form –>
<?php endif; ?>
[/php]
varun says
i need to know whats the name of the model and the view file we create and how are they called and connected …
can i create different templates for eg different forms and put conditions eg ..
if send to client company send _form1
other wise send _form 2??
i am new to yii .. please help me implement the above .. thanks
M.Pathma says
Hi, Ghazali Tajuddin,
Nice work on the YiiMail. Thanks for sharing.
My name is M.Pathma from Promosys Technology, We are looking for PHP Developer using YII Framework under WAMP or XAMPP platform. Basically we are developing WebApps for Monitoring Solution, Promosys is dedicated for Monitoring Solution development. check our more about us at http://www.mypromosys.com
If you have any friends in Klang area looking for job under PHP + WAMP (or XAMPP) please share my contact information with them. they can contact me at mpathma@gmail.com or call in at 03-33231001
M.Pathma
Kaungkhant Mintun says
I am new in Yii.Can I get this source codes,plz sir? :3
Ahmad Ghazali Ahmad Tajuddin says
Yes. Tq.