dongshang1979
2016-10-26 11:34
浏览 49
已采纳

使用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("
");
    return $this->email->send();
}
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dougang7521 2016-10-26 11:44
    已采纳

    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'))
    
    打赏 评论
  • dongliyan9190 2016-10-26 11:45

    Check your code:

    You should change function "send_mail" in your controller because in model you used "sendEmail".

    Change in to your controller :

    $this->blogs_model->sendEmail($this->input->post('email'))
    
    打赏 评论
  • drbz99867 2016-10-26 11:48

    I think the problems in your code is this line smtp_host' => 'ssl://smtp.googlemail.com

    try instead: smtp_host' => 'http://smtp.gmail.com

    打赏 评论

相关推荐 更多相似问题