0

I am trying to make a registration where website sends a confirmation email and users enter this email and continue registering. However, it doesn't send anything to email. Where am I wrong?

Here is my controller.php:

<?php
class user extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url', 'security'));
        $this->load->library(array('session', 'form_validation', 'email'));
        $this->load->database();
        $this->load->model('User_model');
    }

    function index()
    {
        $this->register();
    }

    function register()
    {
        //set validation rules
        $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha|min_length[3]|max_length[30]|is_unique[instructors.instructors_slug]xss_clean');
        $this->form_validation->set_rules('mail', 'Email', 'trim|required|valid_email|is_unique[instructors.mail]');
        $this->form_validation->set_rules('password', 'password', 'trim|required|md5');
        $this->form_validation->set_rules('password2', 'Confirm Password', 'trim|required|md5|matches[password]');

        $data['courses'] = $this->Popular_courses_model->get_popular_courses();
        $data['news'] = $this->News_model->get_news();

        //validate form input
        if ($this->form_validation->run() == FALSE)
        {
            // fails


            $this->load->view('templates/header');
            $this->load->view('pages/index', $data);
            $this->load->view('templates/footer');
        }
        else
        {
            //insert the user registration details into database
            $data = array(
                'instructors_slug' => $this->input->post('username'),
                'mail' => $this->input->post('mail'),
                'password' => $this->input->post('password')
            );

            // insert form data into database
            if ($this->User_model->insertUser($data))
            {
                // send email
                if ($this->User_model->sendEmail($this->input->post('mail')))
                {
                    // successfully sent mail
                    $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
                    redirect('user/register');
                }
                else
                {
                    // error
                    $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                    redirect('user/register');
                }
            }
            else
            {
                // error
                $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error.  Please try again later!!!</div>');
                redirect('user/register');
            }
        }
    }

    function verify($hash=NULL)
    {
        if ($this->User_model->verifyEmailID($hash))
        {
            $this->session->set_flashdata('verify_msg','<div class="alert alert-success text-center">Your Email Address is successfully verified! Please login to access your account!</div>');
            redirect('user/register');
        }
        else
        {
            $this->session->set_flashdata('verify_msg','<div class="alert alert-danger text-center">Sorry! There is error verifying your Email Address!</div>');
            redirect('user/register');
        }
    }
}
?>

Here is my model:

<?php
class user_model extends CI_Model
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    //insert into user table
    function insertUser($data)
    {
        return $this->db->insert('instructors', $data);
    }

    //send verification email to user's email id
    function sendEmail($to_email)
    {
        $from_email = 'support@wtf.az'; //change this to yours
        $subject = 'Verify Your Email Address';
        $message = 'Dear User,<br /><br />Please click on the below activation link to verify your email address.<br /><br /> http://wtf.az/user/verify/' . md5($to_email) . '<br /><br /><br />Thanks<br />Mydomain Team';

        //configure email settings
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'cpanel.freehosting.com'; //smtp host name
        $config['smtp_port'] = '465'; //smtp port number
        $config['smtp_user'] = $from_email;
        $config['smtp_pass'] = '*my password here*'; //$from_email password
        $config['mailtype'] = 'html';
        $config['charset'] = 'iso-8859-1';
        $config['wordwrap'] = TRUE;
        $config['newline'] = "\r\n"; //use double quotes
        $this->email->initialize($config);

        //send mail
        $this->email->from($from_email, 'WTF');
        $this->email->to($to_email);
        $this->email->subject($subject);
        $this->email->message($message);
        return $this->email->send();
    }

    //activate user account
    function verifyEmailID($key)
    {
        $data = array('status' => 1);
        $this->db->where('md5(mail)', $key);
        return $this->db->update('instructors', $data);
    }
}
?>

Here is my view:

<div class="modal-body">

                  <div>
                    <?php echo $this->session->flashdata('msg'); ?>
                  </div>
                  <?php $attributes = array('class' => 'rex-forms', 'name' => 'registrationform'); ?>
                  <?= form_open_multipart('user/register', $attributes); ?>
                      <div class="form-group">
                        <span class="text-danger"><?php echo form_error('username'); ?></span>
                        <input name="username" type="text" class="form-control" placeholder="Имя пользователя">
                      </div>
                      <div class="form-group">
                        <span class="text-danger"><?php echo form_error('mail'); ?></span>
                        <input name="mail" type="email" class="form-control" placeholder="Электронный адрес">
                      </div>
                      <div class="form-group">
                        <span class="text-danger"><?php echo form_error('password'); ?></span>
                        <input name="password" type="password" class="form-control" placeholder="Пароль">
                      </div>
                      <div class="form-group">
                        <input name="password2" type="password" class="form-control" placeholder="Повторный ввод пароля">
                      </div>                    
                  </div>
                  <div class="modal-footer">
                    <button type="submit" name="submitforreg" class="rex-bottom-medium rex-btn-icon">
                        <span class="rex-btn-text">регистрация</span>
                        <span class="rex-btn-text-icon"><i class="fa fa-arrow-circle-o-right"></i></span>
                    </button>  
                  </div>
                  </form>
                </div>
Sparky
  • 94,381
  • 25
  • 183
  • 265
  • What troubleshooting steps have you performed? How do we know if your password is correct, or if your server is able to send email? Have you tested with server's `sendmail` instead of `smtp`? – Sparky Jun 21 '17 at 20:01
  • Your value for `smtp_host` does not seem correct. `cpanel.freehosting.com` ? – Sparky Jun 21 '17 at 20:07
  • $config['protocol'] = 'smtp'; $config['smtp_host'] = 'ssl://smtp.gmail.com'; //smtp host name $config['smtp_port'] = '587'; //smtp port number $config['smtp_user'] = $from_email; $config['smtp_pass'] = 'pass'; //$from_email password $config['mailtype'] = 'html'; $config['charset'] = 'iso-8859-1'; $config['wordwrap'] = TRUE; $config['newline'] = "\r\n"; //use double quotes $this->email->initialize($config); –  Jun 21 '17 at 20:13
  • @Sparky also I tried the above code it also did not work. –  Jun 21 '17 at 20:42
  • Randomly trying different settings is not the same as troubleshooting. – Sparky Jun 21 '17 at 20:56
  • Tip File and Class naming https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming – Mr. ED Jun 21 '17 at 21:18
  • Is `News_model` autoloaded? – Tpojka Jun 21 '17 at 22:17
  • I would put the email config stuff in the controller https://www.codeigniter.com/userguide3/libraries/email.html#using-the-email-library this example assumes you are sending the email from one of your controllers. But also have you set your local host sendmail config https://www.youtube.com/watch?v=TO7MfDcM-Ho – Mr. ED Jun 21 '17 at 23:56
  • @Tpojka yes it is –  Jun 22 '17 at 00:03
  • @wolfgang1983 I do not use localhost, I use website server. and I changed the place of code to the controller, it did not do anything –  Jun 22 '17 at 00:04

1 Answers1

0

Not sure if this is the issue, but as per the documentation, the model class name must start with a capital letter.

In your model, try changing this:

class user_model extends CI_Model

to this:

class User_model extends CI_Model
Paolo Forgia
  • 5,804
  • 7
  • 39
  • 55
ottiem
  • 69
  • 3