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'))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮