简体   繁体   中英

Registration email won't send in code igniter

I wrote an function to send an email to the user using the email they put on their registration form. However while testing it the email does not go to the email address entered. I checked to make sure that the function was working by having the email cc to an address, which worked. Below is the function.

View

    <div class="col-lg-4 col-lg-offset-4">
    <h2>Hello There</h2>
    <h5>Please enter the required information below.</h5>     
<?php 
    $fattr = array('class' => 'form-signin');
    echo form_open('/main/register', $fattr); ?>
    <div class="form-group">
      <?php echo form_input(array('name'=>'firstname', 'id'=> 'firstname', 'placeholder'=>'First Name', 'class'=>'form-control', 'value' => set_value('firstname'))); ?>
      <?php echo form_error('firstname');?>
    </div>
    <div class="form-group">
      <?php echo form_input(array('name'=>'lastname', 'id'=> 'lastname', 'placeholder'=>'Last Name', 'class'=>'form-control', 'value'=> set_value('lastname'))); ?>
      <?php echo form_error('lastname');?>
    </div>
    <div class="form-group">
      <?php echo form_input(array('name'=>'email', 'id'=> 'email', 'placeholder'=>'Email', 'class'=>'form-control', 'value'=> set_value('email'))); ?>
      <?php echo form_error('email');?>
    </div>
    <?php echo form_submit(array('value'=>'Sign up', 'class'=>'btn btn-lg btn-primary btn-block')); ?>
    <?php echo form_close(); ?>
</div>

Controller

 public function register()
    {

        $this->form_validation->set_rules('firstname', 'First Name', 'required');
        $this->form_validation->set_rules('lastname', 'Last Name', 'required');    
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');    

        if ($this->form_validation->run() == FALSE) {   
            $this->load->view('header');
            $this->load->view('register');
            $this->load->view('footer');
        }else{                
            if($this->user_model->isDuplicate($this->input->post('email'))){
                $this->session->set_flashdata('flash_message', 'User email already exists');
                redirect(site_url().'main/login');
            }else{

                $clean = $this->security->xss_clean($this->input->post(NULL, TRUE));
                $id = $this->user_model->insertUser($clean); 
                $token = $this->user_model->insertToken($id);                                        

                $qstring = base64_encode($token);                    
                $url = site_url() . 'main/complete/token/' . $qstring;
                $link = '<a href="' . $url . '">' . $url . '</a>'; 


                $message = '';                     
                $message .= '<strong>You have signed up with our website</strong><br>';
                $message .= '<strong>Please click:</strong> ' . $link;                          

                $config = array(
                    'protocol' => 'smtp',
                    'smtp_host' => 'host',
                    'smtp_port' => 'port',
                    'smtp_user' => 'username',
                    'smtp_pass' => 'password');

                $this->load->library('email', $config);

                $this->email->set_newline("/r/n");

                $this->email->from('***@****.com', 'High Ball Run8');
                $this->email->to(set_value("email"));
                $this->email->cc('****@*****.com');

                $this->email->subject('Highball Registration Email Activation');
                $this->email->message($message);
                if ($this->email->send()) {
                       redirect(site_url());
                   }else{
                    show_error($this->email->print_debugger());
                   }
                exit;

Please check following working code

function sendMail()
{
    $config = Array(
  'protocol' => 'smtp',
  'smtp_host' => 'ssl://smtp.googlemail.com',
  'smtp_port' => 465,
  'smtp_user' => 'xxx@gmail.com', // change it to yours
  'smtp_pass' => 'xxx', // change it to yours
  'mailtype' => 'html',
  'charset' => 'iso-8859-1',
  'wordwrap' => TRUE
);

        $message = '';
        $this->load->library('email', $config);
      $this->email->set_newline("\r\n");
      $this->email->from('xxx@gmail.com'); // change it to yours
      $this->email->to('xxx@gmail.com');// change it to yours
      $this->email->subject('Resume from JobsBuddy for your Job posting');
      $this->email->message($message);
      if($this->email->send())
     {
      echo 'Email sent.';
     }
     else
    {
     show_error($this->email->print_debugger());
    }

}

Reference

Have you tried

var_dump(set_value("email")); exit();

To see what the output is? Chances are you are not getting the to address correctly, if the hardcoded CC works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM