Pages

12 Feb 2016

My SQL Database table record export to excel and send mail with attachmnet -Using PHP Codeigniter Framework


Send mail with attachmnet - PHP Codeigniter Framework

function export()
    {
$this->load->helper('date');
$params= $this->input->post('params');
        $query = $this->db->query("select * from tbl_order where orderid in ($params)");

        if(!$query)
            return false;
        // Starting the PHPExcel library
        $this->load->library('PHPExcel');
        //$this->load->library('PHPExcel/IOFactory/PHPExcel_IOFactory');
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
        $objPHPExcel->setActiveSheetIndex(0);
        // Field names in the first row
        $fields = $query->list_fields();
        $col = 0;
        foreach ($fields as $field)
        {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
            $col++;
        }
        // Fetching the table data
        $row = 2;
        foreach($query->result() as $data)
        {
            $col = 0;
            foreach ($fields as $field)
            {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
                $col++;
            }

            $row++;
        }
        $objPHPExcel->setActiveSheetIndex(0);

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$filepath=site_url('css/'.date('dMy').'.xls');
$dateposted = date("Ymdhis");   
$objWriter->save(str_replace(__FILE__, $dateposted.'.xls', __FILE__));
echo "<script>alert('File saved at drive C')</script>";

//Send mail attachment
$this->load->library('email'); // load email library
$this->email->from('info@domainname.com', 'Subject');
$this->email->to('toemmail@domain.com');
$this->email->cc('ccemail@domian.com'); 
$this->email->subject('Subject');
$this->email->attach($dateposted.'.xls');
$this->email->send();
 
    }

No comments:

Post a Comment