2017-07-03 137 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