Here is my code in which sending an email when user signup. It takes 5 minutes or more for every mail to reaches to the user email. I do not know why email taking so much time to reach. if I am sending an email with this same function sendEmail() not with a verification code only with a simple text. Now it takes 1 minute or 1 n half minutes to reach the user email.
Sometimes it does not even send any email when I am sending the verification code at the end of the link.
I do not know how to send the email with SMTP. I found some examples where they add their domain name to the smtp_host, email, password, which email is created with the domain. I did the same but nothing happens with my email sending. It almost same with this also whether I am using SMTP or not.
This is my function name sendEmail() which I have created the model to sending emails. The reason why I have created this function in the model because I have to send emails from other controllers too. I do not if it could be a problem in sending emails
Please see this function where I am doing wrong. or if there is another way please tell me how to do this.or any type of suggestions will be very helpful for me.
Controller
function index() {
//my validation rules are here
if ($this->form_validation->run() == TRUE) {
$data = $this->fetch_data_from_post();
$user_email = $data['email'];
$code = random_string('unique');
$verification_link = base_url('Home/verify/').$code;
$subject = "Confirmation Message";
$message = "Dear User,
Please click on Given below URL to verify your Email Address ".$verification_link."
Once you click on the above link then your account will be verified and you will get an opportunity to login. See you soon,Thank You...!
";
$email_sent = $this->Perfect_mdl->sendEmail($user_email,$subject,$message);
if($email_sent != 1){
$flash = '<div class="alert alert-danger">Opppssss Somthing went Wrong...</div>';
$this->session->set_flashdata('user_registration',$flash);
redirect('Home/signup');
}else{
$this->Perfect_mdl->_insert($data);
$flash = '<div class="alert alert-success">You are Successfully Registered... Please Check Your Email <b>'.$user_email.'</b> For Verification</div>';
$this->session->set_flashdata('user_registration',$flash);
redirect('Home/signup');
}
}
$data['meta_title'] = "Signup";
$data['services_name'] = $this->Perfect_mdl->getServices_home();
$dat['flash'] = $this->session->flashdata('user_registration');
$this->load->view('signup',$data);
}
Model:-
function sendEmail($useremail,$subject,$message) {
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.mydomainname.com',
'smtp_port' => 25,
'smtp_user' => 'vishal@mydomainname.com', // change it to yours
'smtp_pass' => 'vishal123456', // change it to yours
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'crlf' => "
",
'newline' => "
",
'wordwrap' => TRUE
);
$this->load->library('email', $config);
$this->email->from('vishal@mydomainname.com', 'Company Name');
$this->email->to($useremail);
$this->email->subject($subject);
$this->email->message($message);
if($this->email->send()){
return "1";
}else{
return "0";
}
}