  | 
              
	              
	                
  
    
	 
        Codeigniter ติดปัญหาส่งค่าลืมรหัสผ่าน (Forgot Password) แล้วมันไม่เข้าเมล ครับ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                Codeigniter ติดปัญหาส่งค่าลืมรหัสผ่าน (Forgot Password) แล้วมันไม่เข้าเมล ครับ 
ไม่ทราบว่าผิดตรงไหนครับ  
 
ขอบคุณครับ 
 
forgot_password.php (views) 
<?php 
defined('BASEPATH') OR exit('No direct script access allowed'); 
?> 
 
<div style="background-color:#eee; width:100%; margin-top:0px;"> 
  <div class="container"> 
    <div class="login_size"> 
      <div class="card"> 
        <div class="card-block" style="margin:30px auto 30px auto;"> 
          <form method="post" action="" class="form-signin" role="form"> 
           
 <?php if(!empty($notif)){ ?> 
                    <div id="signupalert" class="alert alert-<?php echo $notif['type'];?>"> 
                        <p><?php echo $notif['message'];?></p> 
                        <span></span> 
                    </div> 
                    <?php } ?> 
           
            <h4 class="form-signin-heading">ลืมรหัสผ่าน</h4> 
            <hr /> 
             
             
            <div class="input-group mb-2 mr-sm-2 mb-sm-0"> 
              <div class="input-group-addon"> <img src="<?php echo base_url(); ?>/assets/images/icon_email.jpg" width="16" height="16" /></div> 
              <input type="text" class="form-control" name="email" value="<?php echo $this->input->post('email');?>" id="inlineFormInputGroup" placeholder="Email Address"> 
            </div> 
             
            </fieldset> 
            <input type="submit" class="btn btn-primary" value=" Reset "> 
 
                       <hr/> 
            <div class="form-group" > 
              <div class="col-md-12 control"> 
     <div style="font-size:85%;"> <a href="<?php echo base_url();?>auth/register">สมัครสมาชิก</a> || <a href="<?php echo base_url();?>auth/login">เข้าสู่ระบบ</a> </div> 
              </div> 
            </div> 
             
          </form> 
        </div> 
      </div> 
    </div> 
  </div> 
</div> 
</div>  
 
Auth_model.php(Models) 
public function email_exists($email) { 
        $sql = "SELECT first_name, email FROM users WHERE email = " . $this->db->escape($email); 
        $result = $this->db->query($sql); 
        $row = $result->row(); 
		 
		return ($result->num_rows() === 1 && $row->email) ? $row->first_name : false; 
	    
    }  
 
(controllers) 
 public function forgot_password() { 
        $data['title'] = 'Forgot password'; 
        $this->load->model('auth_model'); 
 
        if (count($_POST)) { 
            $this->load->helper('security'); 
            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); 
             
            if ($this->form_validation->run() == false) { 
                $data['notif']['message'] = validation_errors(); 
                $data['notif']['type'] = 'danger'; 
            }  
            else { 
                $result = $this->auth_model->email_exists($this->input->post('email')); 
                if($result){ 
					 
                    //if we found the email, $result is now their first name 
					$this->send_reset_password_email($email, $result); 
				    redirect(base_url('index')); 
					 
					 
                } 
                else{ 
                    $data['notif']['message'] = 'This email does not exist on the system'; 
                    $data['notif']['type'] = 'danger'; 
                } 
            } 
        } 
 
       
                  $this->load->view('templates/header',$data); 
		          $this->load->view('templates/menu'); 
		          $this->load->view('all_page/auth/forgot_password'); 
		          $this->load->view('templates/footer'); 
    } 
 
 
 
private function send_reset_password_email($email, $first_name) { 
	$this->load->library('email'); 
	 
	$email_code = Utils::hash('sha1', $this->input->post('password'), AUTH_SALT). $first_name; 
	 
	$this->email->set_mailtype('html'); 
	$this->email->from($this->config->item('bot_mail', 'Freight Forum')); 
	$this->email->to($email); 
	$this->email->subject('Plese reset your password at Freight Forum'); 
	 
	$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
</head> 
 
<body> 
</body>'; 
$message .= '<p>Dear ' . $first_name . ',</p>'; 
// the link we send will look like: /login/reset_password_form/[email protected]/d27ffdfkvlfkvlfvfxjfvfv 
$message .= '<p>We want to help you reset your password! Plese <strong><a href="' . base_url() .'auth/reset_password_form/' .$email . '/'. $email_code . '">click here</a></strong> to reset your password.</p>';		 
$message .= '<p>Thank you!</p>';	 
$message .= '<p>The Team at Freight Forum</p>';	 
$message .= '</body></html>'; 
 
$this->email->message($message); 
$this->email->send(); 
	} 
 
 
  Tag : PHP               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2018-01-07 09:31:07	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2018-01-07 09:28:24 | 
                      By :
                          teedesign | 
                      View :
                          2090 | 
                      Reply :
                          3 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ลองใช้ของ gmail ดูครับ                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-01-08 10:52:45 | 
                        By :
                            mangkunzo | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เอา tag php ครอบสิครับ 
 
และตอนสมัคร คอนเฟิร์มเมลยังไงครับ น่าจะเอามาเป็นตัวอย่าง 
 
อันนี้เป็น function ที่ผมใช้งานอยู่ ก็ลองประยุกต์เอานะครับ 
Code (PHP) 
    public function send_mail( $subj, $to, $from, $msg, $cc, $bcc, $noreply){
        $ci = get_instance();
        $ci->load->library('email');
        //if( !$this->is_email($to)){ $this->er('', $to.' is not email.','');}
        ////////////////////////////////////////////////////////
        $config['protocol'] = 'smtp';
        $config['smtp_crypto']='tls';
        $config['smtp_host'] = "smtp.gmail.com";
        $config['smtp_port'] = "25";
        $config['smtp_timeout'] = '7';
        $config['smtp_user'] = "[email protected]";
        $config['smtp_pass'] = "***********";
        $config['smtp_auth'] = true;
        $config['charset'] = "utf-8"; //'iso-8859-1'
        $config['mailtype'] = "html";
        $config['newline'] = "\r\n";		
        $ci->email->initialize($config);
        $ci->email->from('[email protected]');
        $ci->email->to($to);
        if( $cc>''){ $ci->email->cc($cc); }
        if( $bcc>''){ $ci->email->bcc($bcc);}
        if(!$noreply){ $ci->email->reply_to($from);}
        $ci->email->subject($subj);
        $ci->email->message($msg);
        $ci->email->send();
    }
                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2018-01-08 21:58:55              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-01-08 11:21:43 | 
                        By :
                            Chaidhanan | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ขอบคุณคร๊าบบบบได้แล้วคร๊าบบบบ     
 
ผมทำตามนี้ครับ 
http://myrightcode.com/send-email-using-php-mailer-codeigniter/ 
 
แต่เปลี่ยน email.php เป็นแบบส่ง smtp แทนครับ  
 
ส่วนรหัส smtp_pass ผมทำตามนี้ครับ ต้องไปตั้งค่าใน Gmail ครับ เพื่อรับรหัส smtp_pass รับลองส่งเมลไหนก็ไม่เข้า Junk Mail ครับ อิอิอิ 
https://bobandedovic.com/blog/technology/configure-joomla-3x-admin-mail-smtp-gsuite 
 
 
$config['smtp_pass']  = 'lwkohnffvnfdassa';  
 
 
application/config/email.php 
<?php defined('BASEPATH') OR exit('No direct script access allowed.'); 
$config['useragent']        = 'PHPMailer';              // Mail engine switcher: 'CodeIgniter' or 'PHPMailer' 
$config['protocol']         = 'smtp';                   // 'mail', 'sendmail', or 'smtp' 
$config['mailpath']         = '/usr/sbin/sendmail'; 
$config['smtp_host']        = 'smtp.gmail.com'; 
$config['smtp_user']        = '[email protected]'; 
$config['smtp_pass']        = 'lwkohnffvnfdassa'; 
$config['smtp_port']        = 587; 
$config['smtp_timeout']     = 30;                       // (in seconds) 
$config['smtp_crypto']      = 'STARTTLS';                       // '' or 'tls' or 'ssl' 
$config['smtp_debug']       = 0;                        // PHPMailer's SMTP debug info level: 0 = off, 1 = commands, 2 = commands and data, 3 = as 2 plus connection status, 4 = low level data output. 
$config['debug_output']     = '';                       // PHPMailer's SMTP debug output: 'html', 'echo', 'error_log' or user defined function with parameter $str and $level. NULL or '' means 'echo' on CLI, 'html' otherwise. 
$config['smtp_auto_tls']    = true;                     // Whether to enable TLS encryption automatically if a server supports it, even if `smtp_crypto` is not set to 'tls'. 
$config['smtp_conn_options'] = array();                 // SMTP connection options, an array passed to the function stream_context_create() when connecting via SMTP. 
$config['wordwrap']         = true; 
$config['wrapchars']        = 76; 
$config['mailtype']         = 'html';                   // 'text' or 'html' 
$config['charset']          = null;                     // 'UTF-8', 'ISO-8859-15', ...; NULL (preferable) means config_item('charset'), i.e. the character set of the site. 
$config['validate']         = true; 
$config['priority']         = 3;                        // 1, 2, 3, 4, 5; on PHPMailer useragent NULL is a possible option, it means that X-priority header is not set at all, see https://github.com/PHPMailer/PHPMailer/issues/449 
$config['crlf']             = "\n";                     // "\r\n" or "\n" or "\r" 
$config['newline']          = "\n";                     // "\r\n" or "\n" or "\r" 
$config['bcc_batch_mode']   = false; 
$config['bcc_batch_size']   = 200; 
$config['encoding']         = '8bit';                   // The body encoding. For CodeIgniter: '8bit' or '7bit'. For PHPMailer: '8bit', '7bit', 'binary', 'base64', or 'quoted-printable'. 
// DKIM Signing 
// See https://yomotherboard.com/how-to-setup-email-server-dkim-keys/ 
// See http://stackoverflow.com/questions/24463425/send-mail-in-phpmailer-using-dkim-keys 
// See https://github.com/PHPMailer/PHPMailer/blob/v5.2.14/test/phpmailerTest.php#L1708 
$config['dkim_domain']      = '';                       // DKIM signing domain name, for exmple 'example.com'. 
$config['dkim_private']     = '';                       // DKIM private key, set as a file path. 
$config['dkim_private_string'] = '';                    // DKIM private key, set directly from a string. 
$config['dkim_selector']    = '';                       // DKIM selector. 
$config['dkim_passphrase']  = '';                       // DKIM passphrase, used if your key is encrypted. 
$config['dkim_identity']    = '';                       // DKIM Identity, usually the email address used as the source of the email.  
 
 
 
Code 
public function index(){ 
  $this->load->library('email'); 
  $this->email->from('[email protected]@gmail.com', 'Your Name'); 
  $this->email->to('[email protected]'); 
  $this->email->subject('This is my subject'); 
  $this->email->message('This is my message'); 
  $this->email->send(); 
  $this->load->view('welcome_message'); 
}                         
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2018-01-11 09:04:56 | 
                        By :
                            teedesign | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |