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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀