We Know that we have default Contact form in YII , User can entered his query by providing his mail id and clicks submit button,
I have written code for the submit button, which send mail to my maid id along with user provided mail id, to use this please download "PHPMailer-5.2.4" using below link
http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/detail?name=PHPMailer_5.2.4.zip&can=2&q=
and extract the zip file save it to protected/extensions folder, rename the PHPMailer_5.2.4 to PHPMailer so the path of the folder should be 'protected/extensions/PHPMailer' . Give full permission to folder before proceeding
The code as follows
We have model, "ContactForm" extends CFormModel in our application and we have generated view for contact form in our views '/site/contact.php' located in site folder of views.
Now edit your contact form according to requirement, I left the view as it is .. Now we have move actionContact() method in site controller
The actionContact is modified as follows:
public function actionContact()
{
$model=new ContactForm; // Object created for ContactForm model
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$status=$model->sendMail(); // User Defined method, demonstration follows
if($status===0) // If Mail sent failed the code 0 is returned by above method
Yii::app()->user->setFlash('ERROR','Mail Sending Error Please contact Administrator');
else
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
}
}
$this->render('contact',array('model'=>$model));
}
Now coming to the method in ContactForm model , add the below method to the model note that $this is contactform.
public function sendMail(){
require_once(Yii::app()->basePath.'/extensions/PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
// Dialog::message('Mail', "Mail object created with $this->email");
$uname=Yii::app()->user->name;
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'ssl';
$mail->Host = 'mail.nic.in';
$mail->Port = 465; // give SMTP port, I have given my organisation port
$mail->Username = 'xxxxx@xxx.xx';
$mail->Password = '************'; // provide password from the above username the
// mail is sent
$mail->SMTPAuth = true; // Set false if there is no SMTPAuth
$mail->From = 'xxxxx@xxx.xx'; // same as Username .. it is displayed in from field of mail
$ipadd='';
$ipadd=$this->getRealIp(); // obtaining IP address of the client
// $mail->FromName = 'xxxx';
/* $mail->From = "$this->email"; This displays the mail id of the contact form provided by
user in from field of mail, I dont want this in from field so
commented this if one want this Just comment the above
one and use this */
$mail->FromName = "$this->name";
$mail->AddAddress('xxxxx@xxx.xx'); // our mail address to which we are going to receive
// complaints/issues or suggestions
$mail->AddReplyTo("$this->email", "$this->name"); // The response sent to user mail
// address
$ip=Yii::app()->request->userHostAddress;
$mail->IsHTML(true);
$mail->Subject = "regarding $this->subject";
$mail->AltBody = "Mail From HYD Urban NIC Server. To view the message, please use an HTML compatible email viewer!";
$mail->Body = "From Email : $this->email <br/> Logged in fms as <b>$uname</b>";
if(($ipadd!=null)and($ipadd!=''))
$mail->Body= $mail->Body." through Ip address $ipadd or $ip <br/> Content: ".$this->body;
else
$mail->Body= $mail->Body."<br/> Content: ".$this->body;
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
return 0;
}
else
{
echo "Message sent!\n";
return 1;
}
}
The above getRealIp() can be used if needed, this is to trace from which machine the request/complaint fired. once the submit clicked the user details and the form entries comes to our mail and when we click reply the user email address added automatically to the mail as we have given 'AddReplyTo' in our method.
Hope this helps!!!
I have written code for the submit button, which send mail to my maid id along with user provided mail id, to use this please download "PHPMailer-5.2.4" using below link
http://code.google.com/a/apache-extras.org/p/phpmailer/downloads/detail?name=PHPMailer_5.2.4.zip&can=2&q=
and extract the zip file save it to protected/extensions folder, rename the PHPMailer_5.2.4 to PHPMailer so the path of the folder should be 'protected/extensions/PHPMailer' . Give full permission to folder before proceeding
The code as follows
We have model, "ContactForm" extends CFormModel in our application and we have generated view for contact form in our views '/site/contact.php' located in site folder of views.
Now edit your contact form according to requirement, I left the view as it is .. Now we have move actionContact() method in site controller
The actionContact is modified as follows:
public function actionContact()
{
$model=new ContactForm; // Object created for ContactForm model
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$status=$model->sendMail(); // User Defined method, demonstration follows
if($status===0) // If Mail sent failed the code 0 is returned by above method
Yii::app()->user->setFlash('ERROR','Mail Sending Error Please contact Administrator');
else
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
}
}
$this->render('contact',array('model'=>$model));
}
Now coming to the method in ContactForm model , add the below method to the model note that $this is contactform.
public function sendMail(){
require_once(Yii::app()->basePath.'/extensions/PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
// Dialog::message('Mail', "Mail object created with $this->email");
$uname=Yii::app()->user->name;
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'ssl';
$mail->Host = 'mail.nic.in';
$mail->Port = 465; // give SMTP port, I have given my organisation port
$mail->Username = 'xxxxx@xxx.xx';
$mail->Password = '************'; // provide password from the above username the
// mail is sent
$mail->SMTPAuth = true; // Set false if there is no SMTPAuth
$mail->From = 'xxxxx@xxx.xx'; // same as Username .. it is displayed in from field of mail
$ipadd='';
$ipadd=$this->getRealIp(); // obtaining IP address of the client
// $mail->FromName = 'xxxx';
/* $mail->From = "$this->email"; This displays the mail id of the contact form provided by
user in from field of mail, I dont want this in from field so
commented this if one want this Just comment the above
one and use this */
$mail->FromName = "$this->name";
$mail->AddAddress('xxxxx@xxx.xx'); // our mail address to which we are going to receive
// complaints/issues or suggestions
$mail->AddReplyTo("$this->email", "$this->name"); // The response sent to user mail
// address
$ip=Yii::app()->request->userHostAddress;
$mail->IsHTML(true);
$mail->Subject = "regarding $this->subject";
$mail->AltBody = "Mail From HYD Urban NIC Server. To view the message, please use an HTML compatible email viewer!";
$mail->Body = "From Email : $this->email <br/> Logged in fms as <b>$uname</b>";
if(($ipadd!=null)and($ipadd!=''))
$mail->Body= $mail->Body." through Ip address $ipadd or $ip <br/> Content: ".$this->body;
else
$mail->Body= $mail->Body."<br/> Content: ".$this->body;
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
return 0;
}
else
{
echo "Message sent!\n";
return 1;
}
}
public function getRealIp() // To obtain real IP address
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
The above getRealIp() can be used if needed, this is to trace from which machine the request/complaint fired. once the submit clicked the user details and the form entries comes to our mail and when we click reply the user email address added automatically to the mail as we have given 'AddReplyTo' in our method.
Hope this helps!!!