Advertisement
Guest User

Send Email With CodeIgniter

a guest
May 15th, 2015
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.36 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3.  
  4.      
  5. class Guestbook extends CI_Controller {
  6.  
  7.    /**
  8.     * ----------------------------------------------------------------------
  9.     * Method   __construct()
  10.     * @return  void
  11.     * @access  public        
  12.     * ----------------------------------------------------------------------
  13.     */    
  14.     public function __construct()
  15.     {
  16.         parent::__construct();
  17.         $this->load->model(array('m_gbook'));        
  18.         $this->load->helper(array('captcha'));
  19.          
  20.         date_default_timezone_set('Asia/Jakarta');    
  21.     }
  22.  
  23.    /**
  24.     * ----------------------------------------------------------------------
  25.     * Method   index()
  26.     * @return  void
  27.     * @access  public
  28.     * fungsi untuk menampilkan form input buku tamu    
  29.     * ----------------------------------------------------------------------
  30.     */ 
  31.     public function index()
  32.     {      
  33.         $data['setting']    = $this->m_config->find_by_id(1);
  34.         $data['action']     = site_url('guestbook/save/');
  35.         $data['captcha']    = $this->_set_captcha();
  36.         $data['multilevel'] = $this->m_nav->get_data();                        
  37.         $data['content']    = 'guestbook/form_guestbook';
  38.         $this->load->view('front_end/index', $data);                   
  39.     }
  40.  
  41.    /**
  42.     * ----------------------------------------------------------------------
  43.     * Method   save()
  44.     * @return  void
  45.     * @access  public
  46.     * fungsi untuk menyimpan data buku tamu    
  47.     * ----------------------------------------------------------------------
  48.     */    
  49.     public function save()
  50.     {
  51.         if($this->_set_validation() == TRUE)
  52.         {
  53.             $this->m_gbook->fill_data();
  54.             $this->m_gbook->insert();
  55.            
  56.             $data['multilevel'] = $this->m_nav->get_data();
  57.             $data['type']    = 'success';                
  58.             $data['message'] = 'Pesan sudah tersimpan!';
  59.             $data['setting'] = $this->m_config->find_by_id(1);
  60.             $data['content'] = 'alert/front_alert';
  61.             $this->load->view('front_end/index', $data);
  62.             echo "<meta http-equiv='refresh' content='2; url=".site_url()."/guestbook/'>";            
  63.         }
  64.         else
  65.         {
  66.             $this->index();
  67.         }
  68.     }
  69.    
  70.    /**
  71.     * ----------------------------------------------------------------------
  72.     * Method   _set_validation()
  73.     * @return  boolean
  74.     * @access  public
  75.     * fungsi untuk validasi form inputan    
  76.     * ----------------------------------------------------------------------
  77.     */
  78.     private function _set_validation()
  79.     {                
  80.         $this->form_validation->set_rules('name',    '|', 'trim|required');
  81.         $this->form_validation->set_rules('email',   '|', 'trim|required|valid_email');
  82.         $this->form_validation->set_rules('msg',     '|', 'trim|required');                      
  83.         $this->form_validation->set_rules('captcha', '|', 'trim|required|callback_valid_captcha');
  84.        
  85.         $this->form_validation->set_error_delimiters('<span style="color:maroon; font-weight: normal;">', '</span>');
  86.        
  87.         return $this->form_validation->run();
  88.     }
  89.    
  90.    /**
  91.     * ----------------------------------------------------------------------
  92.     * Method   read()
  93.     * @return  void
  94.     * @access  public
  95.     * fungsi untuk menampilkan data dari tabel tbl_guestbook  
  96.     * ----------------------------------------------------------------------
  97.     */    
  98.     public function read()
  99.     {    
  100.         $this->auth->admin_restrict();            
  101.         /*
  102.         $this->load->library('pagination');
  103.         $config = array(
  104.                  'base_url'       => site_url().'/guestbook/read/',
  105.                  'total_rows'     => $this->db->count_all('tbl_guestbook'),
  106.                  'per_page'       => 10,
  107.                  'uri_segment'    => 3,
  108.                  
  109.                  'first_link'     => 'First',
  110.                  'last_link'      => 'Last',
  111.                  'next_link'      => 'Next',
  112.                  'prev_link'      => 'Prev',
  113.                  'full_tag_open'  => '<ul>',
  114.                  'full_tag_close' => '</ul>',
  115.                  'num_tag_open'   => '<li>',
  116.                  'num_tag_close'  => '</li>',
  117.                  'cur_tag_open'   => '<li class="active"><a>',
  118.                  'cur_tag_close'  => '</a></li>'          
  119.                  );        
  120.         $this->pagination->initialize($config);
  121.         $data['pagination'] = $this->pagination->create_links();
  122.         */        
  123.         $data['dashboard'] = 'class = active';        
  124.         $data['gbooks']  = $this->m_gbook->find_all();
  125.         $data['content'] = 'guestbook/read';
  126.         $this->load->view('back_end/index', $data);
  127.     }
  128.  
  129.    /**
  130.     * ----------------------------------------------------------------------
  131.     * Method   detail()
  132.     * @return  void
  133.     * @access  public
  134.     * fungsi untuk menampilkan 1 record dari tabel tbl_guestbook  
  135.     * ----------------------------------------------------------------------
  136.     */    
  137.     public function detail()
  138.     {
  139.         $this->auth->admin_restrict();  
  140.         if($this->uri->segment(3) == FALSE)
  141.         {
  142.             redirect('guestbook/read/');
  143.         }
  144.         else
  145.         {
  146.             $id = $this->uri->segment(3);
  147.             $data['dashboard'] = 'class = active';            
  148.             $data['query']     = $this->m_gbook->find_by_id($id);
  149.             $data['content']   = 'guestbook/detail';
  150.             $this->load->view('back_end/index', $data);    
  151.         }                
  152.     }
  153.  
  154.    /**
  155.     * ----------------------------------------------------------------------
  156.     * Method   Replay()
  157.     * @return  void
  158.     * @access  public
  159.     * fungsi untuk menampilkan form email reply pada daftar  
  160.     * ----------------------------------------------------------------------
  161.     */    
  162.     public function reply()
  163.     {
  164.         $this->auth->admin_restrict();  
  165.         if($this->uri->segment(3) == FALSE)
  166.         {
  167.             redirect('guestbook/read/');
  168.         }
  169.         else
  170.         {
  171.             $id = $this->uri->segment(3);
  172.             $data['dashboard'] = 'class = active';            
  173.             $data['query']     = $this->m_gbook->find_by_id($id);
  174.             $data['content']   = 'guestbook/detail';
  175.             $this->load->view('back_end/index', $data);    
  176.         }                
  177.     }
  178.  
  179.  
  180.    /**
  181.     * ----------------------------------------------------------------------
  182.     * Method   delete()
  183.     * @return  void
  184.     * @access  public
  185.     * fungsi untuk menghapus data  
  186.     * ----------------------------------------------------------------------
  187.     */      
  188.     public function delete()
  189.     {              
  190.         $this->auth->admin_restrict();      
  191.         $id = $this->uri->segment(3);
  192.         $this->m_gbook->delete($id);
  193.        
  194.         $data['dashboard']     = 'class = active';
  195.         $data['type']          = 'alert-success';                
  196.         $data['alert_heading'] = 'Sukses';                
  197.         $data['message'] = 'Data sudah terhapus!';
  198.         $data['content'] = 'alert/message';
  199.         $this->load->view('back_end/index', $data);
  200.         echo "<meta http-equiv='refresh' content='1; url=".site_url()."/guestbook/read/'>";
  201.     }
  202.  
  203.    /**
  204.     * ----------------------------------------------------------------------
  205.     * Method   multiple_delete()
  206.     * @return  void
  207.     * @access  public
  208.     * fungsi untuk menghapus data  
  209.     * ----------------------------------------------------------------------
  210.     */    
  211.     public function multiple_delete()
  212.     {          
  213.         $this->auth->admin_restrict();  
  214.         if(isset($_POST['id']))
  215.         {
  216.             $id = $_POST['id'];
  217.             $jumlah = count($id);        
  218.            
  219.             for($i = 0; $i<$jumlah; $i++)
  220.             {        
  221.                 mysql_query("DELETE FROM tbl_guestbook WHERE gb_id='$id[$i]' ");                          
  222.             }            
  223.            
  224.             $data['dashboard']     = 'class = active';            
  225.             $data['type']          = 'alert-success';                
  226.             $data['alert_heading'] = 'Sukses';                
  227.             $data['message']       = 'Data sudah terhapus!';
  228.             $data['content']       = 'alert/message';
  229.             $this->load->view('back_end/index', $data);
  230.             echo "<meta http-equiv='refresh' content='1; url=".site_url()."/guestbook/read/'>";                        
  231.         }
  232.         else
  233.         {
  234.             redirect('guestbook/read/');
  235.         }              
  236.     }        
  237.    
  238.     private function _set_captcha()
  239.     {  
  240.         $vals = array(
  241.                 'img_path'   => './assets/captcha/',
  242.                 'img_url'    => base_url().'/assets/captcha/',
  243.                 'img_width'  => 150,
  244.                 'img_height' => 35,
  245.                 'expiration' => 7200,
  246.                 'word'       => random_string('numeric', 4)
  247.                 );     
  248.         $cap  = create_captcha($vals);     
  249.         $data = array(
  250.                 'captcha_time' => $cap['time'],
  251.                 'ip_address'   => $this->input->ip_address(),
  252.                 'word'         => $cap['word']
  253.                 );     
  254.         $query = $this->db->insert_string('captcha', $data);
  255.         $this->db->query($query);
  256.         return $cap;
  257.     }
  258.    
  259.     // OK
  260.     public function valid_captcha($str)
  261.     {      
  262.         // First, delete old captchas
  263.         $expiration = time()-7200; // Two hour limit
  264.         $this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
  265.        
  266.         // Then see if a captcha exists:
  267.         $sql   = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
  268.         $binds = array($str, $this->input->ip_address(), $expiration);
  269.         $query = $this->db->query($sql, $binds);
  270.         $row   = $query->row();
  271.  
  272.         if ($row->count == 0)
  273.         {
  274.             $this->form_validation->set_message('valid_captcha', 'Captcha tidak valid');
  275.             return FALSE;
  276.         }
  277.         else
  278.         {
  279.             return TRUE;
  280.         }
  281.     }
  282.    
  283.     public function excel()
  284.     {
  285.         error_reporting(E_ALL);
  286.        
  287.         $this->load->helper('excel');
  288.         $this->config->load('application_info');
  289.        
  290.         require_once APPPATH.'third_party/PHPExcel'.EXT;  
  291.                      
  292.         $query = $this->m_gbook->excel_data();
  293.                
  294.         $objXLS = new PHPExcel();
  295.         $objSheet = $objXLS->setActiveSheetIndex(0);
  296.        
  297.         $cell = 2;        
  298.         $no   = 1;
  299.         $objSheet->setCellValue('A1', 'No.');
  300.         $objSheet->setCellValue('B1', 'Nama');
  301.         $objSheet->setCellValue('C1', 'Email');
  302.         $objSheet->setCellValue('D1', 'Tanggal Kirim');
  303.         $objSheet->setCellValue('E1', 'Komentar');
  304.         $objSheet->setCellValue('F1', 'IP Address');
  305.        
  306.         foreach($query->result_array() as $data)
  307.         {            
  308.             $objSheet->setCellValue('A'.$cell, $no);
  309.             $objSheet->setCellValue('B'.$cell, $data['gb_name']);
  310.             $objSheet->setCellValue('C'.$cell, $data['gb_email']);
  311.             $objSheet->setCellValue('D'.$cell, indo_date($data['gb_date']));
  312.             $objSheet->setCellValue('E'.$cell, strip_tags($data['gb_content']));
  313.             $objSheet->setCellValue('F'.$cell, $data['gb_ip']);
  314.             $cell++;
  315.             $no++;    
  316.         }
  317.                        
  318.         $file_name = 'Data Inbox';            
  319.         $objXLS->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
  320.         $objXLS->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
  321.         $objXLS->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
  322.         $objXLS->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
  323.         $objXLS->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
  324.         $objXLS->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
  325.         $objXLS->setActiveSheetIndex(0);  
  326.         $objXLS->getProperties()->setCreator($this->config->item('creator'))
  327.                                 ->setLastModifiedBy($this->config->item('creator'))
  328.                                 ->setTitle($file_name)
  329.                                 ->setSubject($this->config->item('subject'))
  330.                                 ->setDescription($file_name)
  331.                                 ->setKeywords($this->config->item('key_word'))
  332.                                 ->setCategory('Data Master');      
  333.                                
  334.         $objWriter = PHPExcel_IOFactory::createWriter( $objXLS, 'Excel5');
  335.  
  336.         header('Content-Type: application/vnd.ms-excel');
  337.         header('Content-Disposition: attachment;filename="'.$file_name.'.xls"');
  338.         header('Cache-Control: max-age=0');
  339.         $objWriter->save('php://output');
  340.         exit();
  341.     }
  342.    
  343.     public function kirim_emailX()
  344.     {
  345.        $this->load->helper(array('form', 'url'));
  346.        $this->load->library('upload');
  347.        $this->load->library('email');
  348.        
  349.        //konfigurasi email
  350.        $config = array();
  351.        $config['charset'] = 'utf-8';
  352.        $config['useragent'] = 'Codeigniter'; //bebas sesuai keinginan kamu
  353.        $config['protocol']= "smtp";
  354.        $config['mailtype']= "text";
  355.        $config['smtp_host']= "ssl://smtp.gmail.com";
  356.        $config['smtp_port']= "465";//465
  357.        $config['smtp_timeout']= "5";
  358.        $config['smtp_user']= "psb.bukhari@gmail.com";//isi dengan email kamu
  359.        $config['smtp_pass']= "psbbukhari@8856"; // isi dengan password kamu
  360.        $config['crlf']="\r\n";
  361.        $config['newline']="\r\n";
  362.        
  363.        $config['wordwrap'] = TRUE;
  364.        //memanggil library email dan set konfigurasi untuk pengiriman email
  365.        
  366.        $this->email->initialize($config);
  367.        //konfigurasi pengiriman
  368.        $this->email->from($this->input->post('from'));
  369.        $this->email->to($this->input->post('to'));
  370.        $this->email->subject($this->input->post('subject'));
  371.        $this->email->message($this->input->post('isi'));
  372.        //Configure upload.
  373.        
  374.        $this->upload->initialize(array(
  375.                             "upload_path"   => "./uploads/",
  376.        "allowed_types" => "*"
  377.        ));
  378.        
  379.    
  380.        if($this->email->send())
  381.        {
  382.         //echo "berhasil mengirim email";
  383.             redirect('guestbook/read');
  384.        }else
  385.        {
  386.         //echo "gagal mengirim email";
  387.         echo $this->email->print_debugger();
  388.        }
  389.        
  390.     }
  391.  
  392.     //on
  393.     public function send_now()
  394.     {
  395.         $this->load->library('email');
  396.  
  397.         $this->email->from('no.replay@psb-bukhari.com', 'Panitia Seleksi');
  398.         $this->email->to('liliek.triyono@gmail.com');
  399.         $this->email->cc('');
  400.         $this->email->bcc('');
  401.  
  402.         $this->email->subject('Sending Email from CodeIgniter with Mandrill');
  403.         $this->email->message('If you forgot how to do this, go ahead and refer to: <a href="http://the-amazing-php.blogspot.com/2015/05/codeigniter-send-email-with-mandrill.html">The Amazing PHP</a>.');
  404.  
  405.         //$this->email->send();
  406.  
  407.         if (!$this->email->send()){
  408.           show_error($this->email->print_debugger());
  409.         }else{ echo 'YEAH!!!';}
  410.     }
  411.    
  412.     //on
  413.     public function send_emailx()
  414.     {
  415.         $to = "liliek.triyono@gmail.com";//need to change
  416.         $subject = "Email subjet here";
  417.         $from_name = "4 Rapid Development";
  418.         $from_email = "no-reply@4rapiddev.com";
  419.  
  420.         $body = "";
  421.         $body .= "Hi,<br>";
  422.         $body .= "How are you?<br><br>";
  423.         $body .= "<a href='http://4rapiddev.com'>4rapiddev.com</a>";
  424.  
  425.         $this->load->library('email');
  426.  
  427.         $config['charset'] = 'utf-8';
  428.         $config['mailtype'] = 'html';
  429.         $config['wordwrap'] = TRUE;
  430.         $config['protocol'] = 'smtp';
  431.         $config['smtp_port'] = '587';//need to change; Mandrill: 587
  432.         $config['smtp_host'] = 'smtp.mandrillapp.com';//need to change; Mandrill: smtp.mandrillapp.com
  433.         $config['smtp_user'] = "psb.bukhari@gmail.com";//need to change
  434.         $config['smtp_pass'] = "eolzZHCxlAr4DdA3MZehyg";//need to change
  435.         $config['smtp_timeout'] = "30";
  436.  
  437.         $this->email->initialize($config);
  438.  
  439.         $this->email->from('no-reply@4rapiddev.com', '4 Rapid Development');
  440.         $this->email->to($to);
  441.         $this->email->subject($subject);
  442.         $this->email->message($body);
  443.  
  444.         //$this->email->send();  
  445.         if (!$this->email->send()){
  446.           show_error($this->email->print_debugger());
  447.         }else{ echo 'YEAH!!!';}
  448.     }
  449.  
  450.     //on
  451.     public function kirim_email()
  452.     {
  453.         $to = $this->input->post('to');//need to change
  454.         $subject = $this->input->post('subject');
  455.         $content = $this->input->post('isi');
  456.         $from_name = "Panitia Seleksi PSB Al Bukhari";
  457.         $from_email = "no-reply@srv-psb.com";
  458.        
  459.  
  460.         $this->load->library('email');                                
  461.         $this->email->from($from_email, $from_name);            
  462.         $this->email->to($to);
  463.         $this->email->set_mailtype('html');              
  464.         $this->email->subject($subject);              
  465.         $this->email->message($content);
  466.        
  467.         if($this->email->send())
  468.         {
  469.             //echo "berhasil mengirim email";
  470.             redirect('guestbook/read');
  471.         }else
  472.         {
  473.             //echo "gagal mengirim email";
  474.             echo $this->email->print_debugger();
  475.         }
  476.     }
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement