MailIgniter中的邮件功能不起作用

时间:2017-07-03 11:56:35

标签: php codeigniter

在这段代码中,我创建了一个带有简单邮件功能的验证码,但无效。

当我点击注册时,我想向收件人发送邮件。

controller: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: example@gmail.com";
                        $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);
        }
    }

view: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>

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:0)

我修改了你的代码。使用CodeIgniter的内置库。替换那部分

$subject = "testing";
$txt = "Hello world!";
$headers = "From: example@gmail.com";
$success = mail($to,$subject,$txt,$headers);

使用

 $toEmail = $this->input->post('email');
 $fromEmail = "example@gmail.com";

 // 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