Thursday, December 4, 2014

send email YII

Standard



In this tutorial, how to send user email & admin email .most developers are doesn't like  use extension i think that is good because that extension may be  have unnecessary things we need .but extension  is very helpful for reduce our coding time  .for this complete mr. gayan senjeewa  help  & i really appreciate for that .thanks 

here code

i used to controller file on components folder because.All controller classes for this application should extend from this base class.

this is main email sending function .add this on  controller 



 
//Controller in confuguration
 public static function email($userName, $userEmail, $subject, $message, $toAdmin = FALSE, $isCC = FALSE) {
            $subject='=?UTF-8?B?'.base64_encode($subject).'?=';
            $to = '';
            $from = '';
            $cc = '';
            
            if ($toAdmin) {
                $to = 'admin@mail.com';//admin Email
                $from = 'supportemail@mail.com'; //support user email for 

                
            } else {
                $to = $userEmail;
                $from =  'admin@mail.com';//admin Email
            }
            
            if($isCC) {
                $headers="From: <{$from}>\r\n".
                        "Cc: {$from}\r\n".
                        "Reply-To: {$from}\r\n".
                        "MIME-Version: 1.0\r\n".
                        "Content-Transfer-Encoding: 8bit\r\n".
                        "Content-Type: text/html; charset=ISO-8859-1\r\n";
            } else {
                $headers="From: <{$from}>\r\n".
                        "Reply-To: {$from}\r\n".
                        "MIME-Version: 1.0\r\n".
                        "Content-Transfer-Encoding: 8bit\r\n".
                        "Content-Type: text/html; charset=ISO-8859-1\r\n";
            }
            
            return mail($to, $subject,$message,$headers);
        }

i update actionContact funtion


 public function actionContact()
 {
  $model=new ContactForm;
  if(isset($_POST['ContactForm']))
  {

//   $model->attributes=$_POST['ContactForm'];
   if($model->validate())
   {
//    $name='=?UTF-8?B?'.base64_encode($model->name).'?=';
//    $subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
//    $headers="From: $name <{$model->email}>\r\n".
//     "Reply-To: {$model->email}\r\n".
//     "MIME-Version: 1.0\r\n".
//     "Content-Type: text/plain; charset=UTF-8";
//
//    mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);
//    
                $name = $model->name;
                $email = $model->email;
                $Telephone = '0715977097';
                $to = 'admin@mail.com';
                $subject = $model->subject;
                    
                $messageUser = "Thank you for contacting us";
                      
                $messageAdmin = "
                Deal Admin,   
                
                you have email from   $model->name from $model->email
        
                $model->body
                thank you
                    
                ";
                self::email($name, $email, $subject, $messageUser);//send mail to user
                self::email($name, $email, $subject, $messageAdmin, TRUE);//send mail to admin  
    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));
 }

0 comments:

Post a Comment