user7047368 user7047368 - 1 month ago 15
MySQL Question

Sending Email after insertion of data using codeigniter php

Sending email after inserting the data into database using

codeigniter
PHP
is not working.Data is inserting succesfully but the
MAIL
functionality is not working getting as
www.hostname.com
page isn’t working.Can any one help me this.Thanks in advance.Here is my code.

Controller:

class Blog extends CI_Controller
{
function __construct()
{
parent::__construct();
//here we will autoload the pagination library
$this->load->model('blogs_model');
$this->load->library('email');
}
function addcomments()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('first_name','First Name' , 'required');
$this->form_validation->set_rules('email','Email');
$this->form_validation->set_rules('location','Location');
$this->form_validation->set_rules('description','Description');
if($this->form_validation->run()== FALSE)
{
$data['mainpage']='blogs';
$this->load->view('templates/template',$data);
}
else
{

//insert the user registration details into database
$data=array(
'blog_id'=>$this->input->post('bl_id'),
'first_name'=>$this->input->post('first_name'),
'email'=>$this->input->post('email'),
'description'=>$this->input->post('description'),
'location'=>$this->input->post('location')
);
// insert form data into database
if ($this->blogs_model -> insertcomments($data))
{
// send email
if ($this->blogs_model->send_mail($this->input->post('email')))
{
// successfully sent mail
$this->flash->success('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
redirect("blog");
}
else
{
// error
$this->flash->success('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect("blog");
}
}
else
{
// error
$this->flash->success('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('blog');
}
}
}
}


Model:

function insertcomments($data)
{
return $this->db->insert('comments', $data);
//$this->db->insert('comments',$data);
//return $this->input->post('bl_id');
}

function sendEmail($to_email)
{

//configure email settings
$config=Array(
'protocol'=> 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com', //smtp host name
'smtp_port' => '465', //smtp port number
'smtp_user' => 'xxxx@gmail.com',
'smtp_pass' => '************', //$from_email password
'mailtype' =>'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);

//send mail
$this->load->library('email',$config);
$this->email->from('xxxx@gmail.com', 'Admin');
$this->email->to('yyy@gmail.com');
$this->email->subject('Comments');
$this->email->message('Testing');
$this->email->set_newline("\r\n");
return $this->email->send();
}

Answer

You are using wrong method name for sending email in your controller:

$this->blogs_model->send_mail($this->input->post('email'))

Correct function name is sendEmail()

$this->blogs_model->sendEmail($this->input->post('email'))