0

I am trying to send a registration verification email to my new user via Gmail SMTP. user data is successfully saved to the database but email is not sent to the user email ID. I am working on localhost

here is my code

$subject = "Please verify email for login";
            $message = "
                        <p>Hi " . $this->input->post('hospital_name') . "</p>
                        <p>This is email verification mail from Codeigniter Login Register system. For complete registration process and login into system. 
                        First you want to verify you email by click this <a href='" . base_url() . "register/verify_email/" . $verification_key . "'>link</a>.</p>
                        <p>Once you click this link your email will be verified and you can login into system.</p>
                        <p>Thanks,</p>
                        ";
            $config  = array(
                'protocol' => 'smtp',
                'smtp_host' => 'tls://smtp.googlemail.com',
                'smtp_port' => 587,
                'smtp_user' => '****',
                'smtp_pass' => '****',
                'mailtype' => 'html',
                'charset' => 'iso-8859-1',
                'wordwrap' => TRUE
            );
            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $this->email->from('****');
            $this->email->to($this->input->post('hospital_email'));
            $this->email->subject($subject);
            $this->email->message($message);
            if ($this->email->send()) {
                $this->session->set_flashdata('message', 'Check in your email for email verification mail');
                redirect('register');
            }else{
                $this->session->set_flashdata('message', 'something wrong');
            }
  • why are you using iso-8859-1 charset? try changing it to UTF-8. Also try adding this: $this->email->set_mailtype("html"); – Ahmed Saleh Jul 22 '20 at 14:36
  • Still not working – Nxt Invent Solutions Jul 22 '20 at 17:33
  • There are so many duplicates of this question on SO, have you checked them? Have you enabled less secure apps in your Gmail account? Eg https://stackoverflow.com/questions/45942833/pear-mail-unable-to-connect-to-gmail-smtp-failed-to-connect-to-socket, https://stackoverflow.com/questions/1555145/sending-email-with-gmail-smtp-with-codeigniter-email-library, https://stackoverflow.com/questions/38417920/unable-to-send-email-using-smtp-gmail-config-in-codeigniter-3, https://stackoverflow.com/questions/39983110/codeigniter-send-mail-with-gmail-smtp-on-localhost, ... – Don't Panic Jul 23 '20 at 00:09

1 Answers1

1

There could be a possibility that there might be something wrong with the code and it would be throwing some error. Did you check in the session, what is the message you're receiving?

If there's an error, try checking what the error could possibly be. You may check the error by using the below code.

if($this->email->send()) {
    echo 'Your Email has successfully been sent.';
}
else{
    show_error($this->email->print_debugger());
}
Aashish gaba
  • 1,726
  • 1
  • 5
  • 14