2017-07-03 135 views
0

在這段代碼中,我創建了一個簡單郵件功能的驗證碼,它不起作用。郵件功能在CodeIgniter中不起作用

當我點擊註冊時,我想發送一封郵件給收件人。

控制器:test.php的

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 
    class Test extends CI_Controller 
    { 
     function __construct() 
     { 
      parent :: __construct(); 
      $this->load->helper(array('form', 'url', 'captcha')); 
      $this->load->model('Fetch_data'); 
      $this->session->set_flashdata(''); 
     } 
     public function login() 
     { 
      if($this->input->post('signup')) 
      { 
       $data = array(
           'firstname' => $this->input->post('fname'), 
           'lastname' => $this->input->post('lname'), 
           'email' => $this->input->post('email'), 
           'phone' => $this->input->post('mobile'), 
           'city' => $this->input->post('city'), 
           'interest_field' => $this->input->post('interest_field'), 
           'password' => $this->input->post('password') 
          ); 
       $this->db->select('*'); 
       $this->db->from('students'); 
       $where = "email = '".$this->input->post('email')."'"; 
       $this->db->where($where); 
       $query = $this->db->get(); 
       if ($query->num_rows() > 1) 
       { 
        $this->session->set_flashdata('message', '<p style="color: red;font-weight: bold;">Email Id already exist. Please login with diffrent email id.</p>'); 
       } 
       else 
       { 
        $inputCaptcha = $this->input->post('captcha'); 
        $sessCaptcha = $this->session->userdata('captchaCode'); 
        if($inputCaptcha === $sessCaptcha) 
        { 
         $this->db->insert('students',$data); 

         $to = $this->input->post('email'); 
         $subject = "testing"; 
         $txt = "Hello world!"; 
         $headers = "From: [email protected]"; 
         $success = mail($to,$subject,$txt,$headers); 
         if($success) 
         { 
          $this->session->set_flashdata('message', '<p style="color: #89c343;font-weight: bold;">Please check your mail for complete registration.</p>'); 
         } 
        } 
        else 
        { 
         $this->session->set_flashdata('message', '<p style="color: red;font-weight: bold;">Captcha code was not match, please try again.</p>'); 
        } 
       } 
      } 
      $config = array(
       'img_path'  => 'resources/img/captcha_images/', 
       'img_url'  => base_url().'resources/img/captcha_images/', 
       'img_width'  => '200', 
       'img_height' => 40, 
       'word_length' => 8, 
       'font_size'  => 16 
      ); 
      $captcha = create_captcha($config); 
      $this->session->unset_userdata('captchaCode'); 
      $this->session->set_userdata('captchaCode',$captcha['word']); 
      $data['captchaImg'] = $captcha['image']; 

      $this->load->view('login',$data); 
     } 
    } 

觀點:login.php中

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" enctype="multipart/form-data"> 
    <div class="form-group"> 
     <input type="text" name="fname" id="fname" placeholder="Your First Name" class="text-line"/> 
    </div> 
    <div class="form-group"> 
     <input type="text" name="lname" id="lname" placeholder="Your Last Name" class="text-line"/> 
    </div> 
    <div class="form-group"> 
     <input type="text" name="email" id="email" placeholder="Your Email" class="text-line"/> 
    </div> 
    <div class="form-group"> 
     <p id="captImg"><?php echo $captchaImg; ?></p> 
     <input type="text" name="captcha" value="" class="captcha" placeholder="Enter Image Inputs"/> 
     <br/> 
     <br/> 
     <a href="javascript:void(0);" class="refreshCaptcha" >Can't Read Please Refersh Image</a> 
    </div> 
    <div class="form-group"> 
     <input type="submit" name="signup" id="signup" value="Sign Up" class="btn btn-warning"/> 
    </div> 
</form> 

回答

0

首先檢查是否您的驗證碼驗證工作,且mail($to,$subject,$txt,$headers);達到。那就試試這個代碼

$this->email->clear(); 
$config['charset'] = 'utf-8'; 
$this->email->initialize($config); 
$this->email->set_newline("\r\n"); 
$this->email->from('YOUR_EMAIL','YOUR_Name'); 
$this->email->to($this->input->post('email')); 
$this->email->subject('testing'); 
$txt = "Hello world!"; 
$this->email->message($txt); 
if($this->email->send()){ 
    $this->session->set_flashdata('message', '<p style="color: #89c343;font-weight: bold;">Please check your mail for complete registration. </p>'); 
} 

希望它可以幫助
編輯
也確保你有負荷電子郵件庫在你的函數的開始或類

$this->load->library('email'); 
+0

驗證碼驗證工作@Regolith但是電子郵件仍然沒有工作 – omkara

+0

你在'localhost'工作?在本地服務器的正常配置下,電子郵件服務器不工作。如果可以,請在實時服務器中檢查。 – Regolith

+0

感謝您的回覆:) – omkara

0

的構建我修改了你的代碼。使用CodeIgniter的內置庫。替換部分

$subject = "testing"; 
$txt = "Hello world!"; 
$headers = "From: [email protected]"; 
$success = mail($to,$subject,$txt,$headers); 

$toEmail = $this->input->post('email'); 
$fromEmail = "[email protected]"; 

// Load email library 
$this->load->library('email'); 

$this->email->from($fromEmail, 'Your Name'); 
$this->email->to($toEmail); 
$this->email->subject('Email Test'); 
$this->email->message('Testing the email class.'); 
$success = $this->email->send(); 

if ($success) { 
     $this->session->set_flashdata('message', '<p style="color: #89c343;font-weight: bold;">Please check your mail for complete registration.</p>'); 
} 

你可以在這裏閱讀更多Email Class

+0

仍然無法使用@Muhammad Usman – omkara

+0

您收到了什麼錯誤? –

+0

當我點擊註冊按鈕表單值插入數據庫,但電子郵件沒有發送。 – omkara